JavaScript Date getTimezoneOffset()

Posted by

JavaScript Date getTimezoneOffset()

The getTimezoneOffset() method returns the time-zone offset from UTC, in minutes, for the current locale. This method returns the time difference between UTC time and local time, in minutes.

Syntax

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

dateObj.getTimezoneOffset()

[/dm_code_snippet]

Description

The local time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local time-zone is behind UTC and negative if it is ahead. For example, if your time-zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned.

Return value

An integer value, indicating the time-zone difference, in minutes, from current locale (host system settings) to UTC.

Example

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let date = new Date();
let offset = date.getTimezoneOffset();
console.log(offset);
// expected output: -180

[/dm_code_snippet]

More Information

The following example shows how to set the time-zone offset from UTC using the getTimezoneOffset() method.

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

let date = new Date();
let offset = date.getTimezoneOffset();

// set the time zone offset in minutes
let timeZoneOffset = offset/60;
let utcTime = date.getTime() + (timeZoneOffset * 60 * 60 * 1000);  

console.log(utcTime);

[/dm_code_snippet]

For more information, see the MDN web docs.