DEV Community

Cover image for Be Careful About Timezones In Backend and Frontend Development
Aqeel-Zeid
Aqeel-Zeid

Posted on

Be Careful About Timezones In Backend and Frontend Development

Dear Devs,

Today I ran into an issue regarding timezones
When I was working on the local host the backend , frontend(browser) and the database all ran on the same machine which is my laptop in a single timezone.

But when I push to production the dates in the backend and the dates in the frontend was 5 hours apart.
This is because the server and the frontend resided in different time zones.

I checked the backend code and the database they were fine , and the frontend code also didn't show any issues.

When I checked my Javascript code it was using

    <h4>{new Date(startTime).toLocaleTimeString()}</h4>
Enter fullscreen mode Exit fullscreen mode

which uses local-time which adds 5 hours to the start time
So instead I used UTC time.

the backend code was using UTC time

*So make sure you pay attention to timezones configurations in your database , backend and frontend according to your use case and your execution environments. *

here are some articles that helped me
https://www.ursahealth.com/new-insights/dates-and-timezones-in-javascript
https://stackoverflow.com/questions/24192558/incorrect-timezone-from-server-side-to-javascript

Photo Credit Donald Wu on Unsplash

Top comments (0)