How to Get the Timezone in JavaScript

Ammar Ali Feb 02, 2024
  1. Get the Timezone and Its Offset Using the Date() Function and the slice() Function in JavaScript
  2. Get the Timezone Offset Using the Date() Function and the getTimezoneOffset() Function in JavaScript
  3. Get the Timezone Using the DateTimeFormat() Function in JavaScript
How to Get the Timezone in JavaScript

This tutorial discusses how to get the timezone and its offset using the Date() function along with the slice(), getTimezoneOffset(), and DateTimeFormat() function in JavaScript.

Get the Timezone and Its Offset Using the Date() Function and the slice() Function in JavaScript

We can use the Date() function to find the timezone and its offset, but it will also return the date and time. For example, let’s use the Date() function to get the current date, time, offset, and timezone. See the code below.

var MyDate = new Date();
console.log(MyDate);

Output:

Wed Jun 23 2021 08:29:17 GMT+0500 (Pakistan Standard Time)

In the output, you can see that the Date() function returned the current date, time, offset, and timezone. If you want to get the timezone or the offset only, you have to convert the output into a string to slice it to get the desired output.

To convert it into a string, we can use the toTimeString() function, which will return the time, offset, and timezone. After that, we can use the slice function to get our desired output. For example, let’s get the timezone offset from the output above. See the code below.

var MyDate = new Date();
console.log(MyDate);
var MyString = MyDate.toTimeString();
console.log(MyString);
var MyOffset = MyString.slice(9, 17);
console.log(MyOffset);

Output:

Wed Jun 23 2021 08:29:17 GMT+0500 (Pakistan Standard Time)
08:39:52 GMT+0500 (Pakistan Standard Time)
GMT+0500

In the output, the second line is returned by the toTimeString() function, and the third line is returned by the slice() function. The first and second argument of the slice() function is the position of the characters that we want to slice.

For example, the slice(9,17) function will start slicing from the 9th character until the 17th character. If you don’t define the second argument, the function will slice until the last character of the string.

Get the Timezone Offset Using the Date() Function and the getTimezoneOffset() Function in JavaScript

We can use the Date() function with the getTimezoneOffset() function to get the timezone offset. This method will return the timezone offset in minutes. See the example code below for a demonstration.

var MyDate = new Date();
console.log(MyDate);
var MyOffset = MyDate.getTimezoneOffset();
console.log(MyOffset);

Output:

Wed Jun 23 2021 08:29:17 GMT+0500 (Pakistan Standard Time)
-300

As you can see in the output, the second line is the timezone offset in minutes. It’s not the same as in the first line because, in there, the offset is in hours. You can also convert it into hours by dividing it with -60. See the code below.

var MyDate = new Date();
console.log(MyDate);
var MyOffset = (MyDate.getTimezoneOffset()) / -60;
console.log(MyOffset);

Output:

Wed Jun 23 2021 08:29:17 GMT+0500 (Pakistan Standard Time)
5

As you can see in the output, the offset in both lines is equal. Note that the getTimezoneOffset() function will only return the offset alone and not with the UTC or GMT.

Get the Timezone Using the DateTimeFormat() Function in JavaScript

We can use the DateTimeFormat() function to get the timezone in JavaScript. For example, see the code below.

var MyTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
console.log(MyTimeZone);

Output:

UTC

The Intl returns the IANA time zone, and it’s compatible with all the modern browsers.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - JavaScript Date