JavaScript

How to Get the Timezone in JavaScript

In JavaScript, getting the Timezone is very useful for fetching the GMT(GreenWich Mean Time) or Universal Time Coordinated (UTC). Moreover, we can view the Timezone of any country around the globe and observe the change in time, date, day, and UTC of that country. In addition to that, TimeZone also assists in calculating the UTC/GMT by observing the time difference.

This manual will offer the method related to getting Timezone in JavaScript.

How to Get the Timezone in JavaScript?

For getting the Timezone in JavaScript, you can utilize:

We will now go through each of the mentioned approaches one by one!

Method 1: Get the Timezone in JavaScript Using Date() Object Method

In JavaScript, Date Object creates a new date object to display the current date/ time. We will utilize the Date object constructor to create a new date object, store it in a variable, and then display the TimeZone.

Syntax

In JavaScript, follow the given syntax for creating a date object:

new Date()

Go through the following example for better understanding.

Example

In this method, we will create a new Date object named “timeZone” using the “Date()” object constructor. Then, we will display the current TimeZone on the console:

var timeZone = new Date();

console.log(timeZone);

Execution of given code will show the following output:

Method 2: Get the Timezone in JavaScript Using Intl.DateTimeFormat() Method

In JavaScript, the mentioned method can be used to enable time formatting and access the time zone. We have also used the “resolvedOptions()” method along with “Intl.DateTimeFormat()”. The “resolvedOptions()” method returns a new object with properties that reflect the locale and date and time formatting options calculated during initialization of this “Intl”.

Syntax

In JavaScript, to use the “Intl.DateTimeFormat()” method for getting the time zone, you have to follow the below-given syntax:

Intl.DateTimeFormat().resolvedOptions().timeZone

Look at the below-given example for demonstration.

Example

First, we will enable the time formatting using the “Intl.DateTimeFormat()” method. Moreover, we will apply the “resolvedOptions()” method along with Intl.DateTimeFormat() in order to reflect the locale and date/time formatting options computed during Intl object initialization. Then, we will display the current time zone on the console.

var timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;

console.log(timeZone);

The corresponding output will be:

We have provided the simplest methods for getting the Timezone in JavaScript. You can use any of the approaches according to your requirements.

Conclusion

To get the timezone in JavaScript, you can use the Date() object constructor for creating a new date object and displaying the timezone or utilize the “Intl.DateTimeFormat()” method for enabling the time formatting and reflecting the date and time formatting options calculated during the initialization of “Intl”. This write-up explained the methods for getting the Timezone in JavaScript.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.