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, local timezone is behind the UTC and if it is negative, the local timezone if 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, date.getTimezoneOffset() method will return the timezone difference in minutes, between the UTC and the local time. This will be stored in offset variable.
Javascript
var date = new Date(); var offset = date.getTimezoneOffset(); |
Example: On clicking the “submit” button, showOffset() method is called which stores the value of timezone offset in offset variable. Then result text is inserted in the empty p tag.
HTML
<!DOCTYPE html> <html> <body> <h2>Timezone offset</h2> <p> Click on submit button to display timezone offset in minutes </p><br> <!--Call showOffset() on clicking the submit button--> <input type="button" value="submit" onclick="showOffset()"> <br><br> <p id="time"></p> <script> function showOffset() { // Date object var date = new Date(); // Offset variable will store // timezone offset between // UTC and your local time var offset = date.getTimezoneOffset(); document.getElementById("time") .innerHTML = "Timezone offset: " + offset + " minutes"; } </script> </body> </html> |
Output:
- Before Clicking the button:
- After Clicking the button:
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());
Recommended Posts:
- How to get the client timezone offset in JavaScript?
- How to create a date with a set timezone without using a string representation?
- How to convert date to another timezone in JavaScript ?
- How to generate a drop down list of timezone using PHP ?
- How to set the offset property for ScrollSpy in Bootstrap ?
- How to get rid of an element offset using CSS ?
- jQuery | offset() with Examples
- CSS | outline-offset Property
- What is the difference between position() and offset() in jQuery ?
- How to avoid undefined offset error in PHP ?
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to set the value of a select box element using JavaScript?
- Set the focus to HTML form element using JavaScript
- Compute the set difference using JavaScript arrays
- How to set font size based on window size using JavaScript ?
- How to set textarea scroll bar to bottom as a default using JavaScript/jQuery ?
- How to set cursor position in content-editable element using JavaScript ?
- How to set smooth scroll after clicking the link using JavaScript ?
- How to map, reduce and filter a Set element using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


