How to set timezone offset using JavaScript ?

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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());

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.