Open In App
Related Articles

How to set timezone offset using JavaScript ?

Improve Article
Improve
Save Article
Save
Like Article
Like

The timezone offset is the time difference in hours or minutes between the Coordinated Universal Time (UTC) and a given time zone.

The 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:

 date.getTimezoneOffset()

Here, date is a JavaScript date object.

Code Snippet: In the below code snippet, the date.getTimezoneOffset() method will return the timezone difference in minutes, between the UTC and the local time. This will be stored in the offset variable. 

Javascript




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

Example: On clicking the “submit” button, showOffset() method is called which 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: -330 minutes

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

// Output will be your local timezone offset 
// It does not depend on date object.

var date = new Date('August 21, 2000 18:02:25 GMT+05:00');
console.log(date.getTimezoneOffset());

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.

Last Updated : 02 Jun, 2023
Like Article
Save Article
Similar Reads
Related Tutorials