JavaScript Date getTimezoneOffset()
Syntax & Examples


Date.getTimezoneOffset() method

The getTimezoneOffset() method of the Date object in JavaScript returns the time-zone offset in minutes for the current locale.


Syntax of Date.getTimezoneOffset()

The syntax of Date.getTimezoneOffset() method is:

getTimezoneOffset()

This getTimezoneOffset() method of Date returns the time-zone offset in minutes for the current locale.

Return Type

Date.getTimezoneOffset() returns value of type Number.



✐ Examples

1 Using getTimezoneOffset() to get the time-zone offset of a specific date

In JavaScript, we can use the getTimezoneOffset() method to get the time-zone offset in minutes from a Date object.

For example,

  1. We create a new Date object date representing January 15, 2021.
  2. We use the getTimezoneOffset() method to get the time-zone offset from date.
  3. The time-zone offset is stored in the variable offset.
  4. We log offset to the console using console.log() method.

JavaScript Program

const date = new Date('2021-01-15');
const offset = date.getTimezoneOffset();
console.log(offset);

Output

-480

2 Using getTimezoneOffset() with the current date

In JavaScript, we can use the getTimezoneOffset() method to get the current time-zone offset in minutes from the current date.

For example,

  1. We create a new Date object now representing the current date and time.
  2. We use the getTimezoneOffset() method to get the current time-zone offset from now.
  3. The current time-zone offset is stored in the variable currentOffset.
  4. We log currentOffset to the console using console.log() method.

JavaScript Program

const now = new Date();
const currentOffset = now.getTimezoneOffset();
console.log(currentOffset);

Output

-420

3 Using getTimezoneOffset() with a specific date in the past

In JavaScript, we can use the getTimezoneOffset() method to get the time-zone offset in minutes from a specific date in the past.

For example,

  1. We create a new Date object pastDate representing July 4, 1776.
  2. We use the getTimezoneOffset() method to get the time-zone offset from pastDate.
  3. The time-zone offset is stored in the variable pastOffset.
  4. We log pastOffset to the console using console.log() method.

JavaScript Program

const pastDate = new Date('1776-07-04');
const pastOffset = pastDate.getTimezoneOffset();
console.log(pastOffset);

Output

-300

Summary

In this JavaScript tutorial, we learned about getTimezoneOffset() method of Date: the syntax and few working examples with output and detailed explanation for each example.