Handling Timezone issues in Javascript

bandi prahasith
2 min readJan 26, 2021

Got clients in all around the time ?? Got issues handling schedules or date-time related issues for all your audiences ?? You are in the right place to get those questions clarified.

It’s 31st December, you are partying hard, waiting for the exciting things to happen in the fresh year, just then you see the flash news coming from the television screening the ANZ’s celebrating the new year. Now you start wondering why would they get the new year vibes before you. Yeah!! That’s how the timezone works.

According to Wikipedia, “A time zone is a designated area of the globe that observes a uniform standard time for legal, commercial and social purposes.

Developers often get bugs related to code which vary their behavior across the globe. To get started with the article, we will first need to understand what a timezone actually is and where it is being used.

Let us assume a scenario where you are working on a development project, where the usecase is to create a meeting between users who are located in different parts of the world. Lets say you want to schedule a two-person meeting, one from India(GMT +5:30) and other person from Pacific time zone(GMT -09:00).

Now that you are created a meeting on 12:00pm UTC on 1st January (after a long hard party) , your code must ensure that the person in pacific time zone must be receiving the request for 03:00 and at 17:30 for the Indian.

This is where the moment library comes in handy. All we need to store in the database is the UTC time, i.e., 2021:01:01:12:00:00Z. Then send the time to both the clients. At the client architecture, use moment library to format time according to the timezone. Moment library grabs on to the ‘Z’ and adds the timezone appropriately for the clients. For the pacific time zone, moment library calculates -09:00 and then shows the user the schedule timing appropriately.

--

--