Logo

dev-resources.site

for different kinds of informations.

Dealing with Timezone in JavaScript

Published at
10/13/2020
Categories
javascript
timezone
webde
date
Author
hudaniabhee
Categories
4 categories in total
javascript
open
timezone
open
webde
open
date
open
Author
11 person written this
hudaniabhee
open
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.

timezone Article's
30 articles in total
Favicon
Converting date by user time zone in "NestJS", and entering and displaying date in "Angular"
Favicon
Timezone support in a full-stack application based on NestJS and Angular: working with REST and WebSockets
Favicon
Python: it is now() time to migrate from utcnow()
Favicon
geo2tz - 4 years later
Favicon
🌐 How to Change Time Zone in Google Chrome to Test Different Timezones
Favicon
¿Cómo trabajar correctamente con fechas?
Favicon
How to fix Dynamics AX 2012 R3 when the time is 1 hour ahead when not using daylight savings time
Favicon
Config Timezone Laravel in Your Project
Favicon
Javascript Zoned-Date library - fully DST support
Favicon
ChronoMate: Your Ultimate Time Zone Companion for Seamless Productivity and Scheduling Ease!
Favicon
supabase timezone + cron
Favicon
Command Prompt - Set Timezone
Favicon
Troubleshooting Timezone Issues in PostgreSQL with DBeaver
Favicon
Fixed: WHM (CPanel) + Laravel Timezone Issue
Favicon
Solve UK time changes (DST) with NodeJS and date-fns and Docker (epoch/unix format)
Favicon
Is there any reason to use ZoneId.of("UTC") instead of ZoneOffset.UTC?
Favicon
Debugging timezone issue in Java [Linux]
Favicon
Local time of employees in Notion
Favicon
Modificando a hora nas configurações do PHP8
Favicon
Time change in daylight saving
Favicon
Avoid this when using Date/Time functions in PHP
Favicon
Timezone for DateTime Field at Laravel Nova
Favicon
NTP (Network Time Protocol) Setup for Linux (Ubuntu) Server
Favicon
[Phoenix LiveView] formatting date/time with local time zone
Favicon
Shift the TZ for your K8s CronJobs
Favicon
Datetimes Are Hard: Part 2 - Writing and running code
Favicon
Where does GMT-0456 Timezone Come From?
Favicon
Dealing with Timezone in JavaScript
Favicon
Date and time gotchas
Favicon
How to avoid and debug most of timezone problems in production

Featured ones: