JavaScript | date.getTimezoneOffset() with Examples

The date.getTimezoneOffset() is an inbuilt function of javaScript which is used to return the time difference between Universal Co-ordinated Time (UTC) and local time, in minutes. If your time zone is GMT+5, -300 (60*5) minutes will be returned. Daylight savings prevent this value from being a constant.
Syntax:

date.getTimezoneOffset()

In the above syntax, date is a valid Date object created by using Date() constructor.
Parameter: It does not accept any parameter.
Return Values: It returns a number representing the time difference between UTC and Local Time in minutes.

JavaScript code to show the working of date.getTimezoneOffset() function:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
    // If nothing is in parameter it takes
    // the current date while creating Date object 
    var date = new Date();
      
    // Extracting the timedifference between
    // UTC and local time
    var diff = date.getTimezoneOffset();
      
   //Printing on console
   document.write(diff); 
  
</script>

chevron_right


Output:

-330

Example 2:
The date of the month should lie in between 1 to 31 because none of the months have a date greater than 31 that is why it returns NaN i.e, not a number because the date for the month does not exist.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
    // Taking the date while creating Date object 
    var date=new Date('Aug 32 2018 5:30');
      
    // Extracting the timedifference between UTC and local time
    var n=date.getTimezoneOffset();
    document.write(n)
  
</script>

chevron_right


Output:

NaN

Note: The Universal Coordinated Time (UTC) is the time set by the World Time Standard. UTC is the same as GMT time.

Supported Browsers: the browsers supported by JavaScript date.getTimezoneOffset() are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


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.