<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=490755098261949&amp;ev=PageView&amp;noscript=1">

NEW CASE STUDY: How we built top-rated shopping apps for Crate & Barrel and CB2 :app:

JavaScript: Handle Date in any Timezone with Daylight saving check

Pratik Agashe
Author Pratik Agashe
Published On May 11, 2020
group_20215

Introduction

JavaScript Date is the most commonly used method. The date plays a significant role in any type of application. Even in our daily life, the date serves a purpose. Now, regarding any application let’s say a blog website. The timestamp is used to denote blog publish date, last edit date, comment date, etc.

Like in our daily lives, the date plays a significant role in any type of application. For example, let’s consider a blog website. Timestamps are used to denote blog publish date, last edit date, comment date, and more.

The most common method for adding a date to your application is JavaScript Date.

1_EuGBdR7ZfEOpFcmozm4D8A

If you want today’s date, you can simply write the following line. This will give you a new object with the current date and time, in your specific timezone.

const d = new Date()
Sat May 02 2020 14:27:09 GMT+0530

Now, in order to use that object, you’ll need to format it using JavaScript Date functions. When it comes to formatting, inputting the date in JavaScript is pretty simple. It will look similar to the following piece of code.

So, inputting a date in your timezone is pretty straightforward. However, managing dates and times gets trickier if you have to display different dates for different countries or cities. For example, an alert functionality may need to get triggered at 16:00 hours every day, in the client's specific time zone.

1__WCnGDFdbj2DOe0aM9GOfA

We can solve this problem in simple 5 steps.

  1. Convert the Date into msec since Jan 1, 1970.
  2. Obtain local UTC offset and convert to msec.
  3. Obtain the current UTC time, by adding the local time zone offset to the local time.
  4. Obtain the destination city’s UTC offset in hours, convert it to milliseconds and add it to UTC time.
  5. Convert final msec value into new Date string.

Quite simple, isn’t it? But there is one more consideration to keep in mind. Namely: will time zone offset adjust when Daylight Saving Time is in effect? The answer is Yes.

If we are considering New York, USA, then during Daylight Saving, the offset is -4, whereas it would otherwise be -5.

There’s a solution for this as well: an additional function while obtaining destination city’s UTC offset. This function will determine if Daylight Saving is in effect (or not), and then return the offset accordingly.

Here’s the code to convert the Date in a different timezone with the Daylight Saving check.

You can see that the transformed Date object will show local offset; that’s because we have created a new Date object with our result msec value. If you look at the Date and Time, you can see the difference.

That’s how you can handle JavaScript Date in different time zones.

Thanks!

Group 215@2x

Interested in a career at Heady?

Excellent! We are always looking for great talent.

app-clips_1
app-clips_2

Interested in a career at Heady?

Excellent! We are always looking for great talent.

LET'S TALK IT OUT.

Figuring out your next step? We're here for you.

Or drop us a line.

hello@heady.io