DEV Community

Abhee Hudani ☕
Abhee Hudani ☕

Posted on • Updated on

Dealing with Timezone in JavaScript

Dealing With Different Timezone in JavaScript

At some point in developer's life he has to deal with different timezones. And I guess it was my turn.

Why we need to deal with timezone ?

So let's just say that, one user from India, one from Canada, and one from Australia makes a new request for 10th January 2020. But the thing is that date is not same for all the users because of the different timezone which they are following.

Let's frame this in another way:

  • You are creating an international cab service, which accepts user's request date and time and according to that cab driver will provide service. If no cab driver accepts user's service before 8 hours then it will be marked as expired.
  • Now you get a call from Australia and a customer will say, by the way we are 9.5 hour ahead from your country. And you will be like okay we can make an exception. Now after 3 hours a customer calls from India and says that by the way we are 4 hours ahead from your country. Now if you think about that, then you will realize that there are more than 24 timezone. So at that time request expiration or deletion would not be easy because at that time we have to deal with all the timezone according to your customer. Because at that time you use the same 10th Jan 2020 -12 Hours Logic then it won't work for the all the customers.

How to deal with timezone ?

So one way to deal this problem is with JavaScript's getTimezoneOffset() function which returns the difference of request date and utc date in minutes.

getTimezoneOffset(): JavaScript getTimezoneOffset() method method returns the time difference between UTC time and local time, in minutes. It can either positive or negative depending upon user's local timezone from his machine.
Enter fullscreen mode Exit fullscreen mode
  • So what one can do is just store the 10th Jan 2020 for all the users as UTC Timestamp and in another DB column you can store the offSet Difference value.

  • So whenever we want to check date for expiration or deletion purpose or any date related operations, we can just subtract the value (which is in minutes) from the original requested date.

  • So this way we can get the requested date in our current date and time. And after that we can find the difference between user's request date (which is now converted to our local timestamp) and current date(local time).

One Another way to deal this is using UNIX Timestamp.

UNIX Time : Unix time is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, minus leap seconds, the Unix epoch is 00:00:00 UTC on 1 January 1970.
Enter fullscreen mode Exit fullscreen mode
  • You can convert user's local time into UNIX time and then you can store it in the database and whenever you want to check for expiration or deletion you will just get the difference between request data's UNIX time and current time UNIX time.

Conclusion

I'm not saying this is 100% fail proof but I this solution worked for me. And I hope it will help you to give you a kick start. It might not be the best solution, but it gets the job done.

Top comments (1)

Collapse
 
sang profile image
Sang

How about this library? dev.to/sang/javascript-zoned-date-...

I have recently published it and would like to hear feedback from users.