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,
- We create a new Date object
daterepresenting January 15, 2021. - We use the
getTimezoneOffset()method to get the time-zone offset fromdate. - The time-zone offset is stored in the variable
offset. - We log
offsetto the console usingconsole.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,
- We create a new Date object
nowrepresenting the current date and time. - We use the
getTimezoneOffset()method to get the current time-zone offset fromnow. - The current time-zone offset is stored in the variable
currentOffset. - We log
currentOffsetto the console usingconsole.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,
- We create a new Date object
pastDaterepresenting July 4, 1776. - We use the
getTimezoneOffset()method to get the time-zone offset frompastDate. - The time-zone offset is stored in the variable
pastOffset. - We log
pastOffsetto the console usingconsole.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.