How to use getTimezoneOffset() method In Javascript

JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, the local timezone is behind the UTC and if it is negative, the local timezone is ahead of UTC. The returned value is not constant if the host system is configured for daylight saving.

Syntax:

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

Example: On clicking the “submit” button, the showOffset() method is called and stores the value of timezone offset in the offset variable. The resulting text is inserted in the empty p tag.

Javascript
function showOffset() {

    // Date object
    let date = new Date();

    // Offset variable will store 
    // timezone offset between 
    // UTC and your local time   
    let offset = date.getTimezoneOffset();

    console.log("Timezone offset: "
        + offset + " minutes");
}
showOffset();

Output
Timezone offset: 0 minutes

Note: The method returns your local timezone offset in minutes and not the timezone offset of the “date” object.


How to set timezone offset using JavaScript ?

The timezone offset represents the difference in minutes between local time and Coordinated Universal Time (UTC). Setting the timezone offset in JavaScript involves obtaining the current date’s timezone offset, measured in minutes, from Coordinated Universal Time (UTC). This offset accounts for variations in time between the local timezone and UTC, allowing accurate time representation in applications.

Similar Reads

Using getTimezoneOffset() method

JavaScript getTimezoneOffset() method is used to find the timezone offset. It returns the timezone difference in minutes, between the UTC and the current local time. If the returned value is positive, the local timezone is behind the UTC and if it is negative, the local timezone is ahead of UTC. The returned value is not constant if the host system is configured for daylight saving....