JavaScript Date getTimezoneOffset() Method
The date.getTimezoneOffset() method is used to return the time difference between Universal Coordinated 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 constant.
Syntax:
date.getTimezoneOffset()
Parameter: This method does not accept any parameter.
Return Values: It returns a number representing the time difference between UTC and Local Time in minutes.
Below are examples of Date.getTimezoneOffset() method.
Example 1:
javascript
// If nothing is in parameter it takes // the current date while creating Date object let date = new Date(); // Extracting the time difference between // UTC and local time let diff = date.getTimezoneOffset(); //Printing on console console.log(diff); |
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.
javascript
// Taking the date while creating Date object let date= new Date( 'Aug 32 2018 5:30' ); // Extracting the timedifference between // UTC and local time let n=date.getTimezoneOffset(); console.log(n) |
Output:
NaN
Example 3: In this example, we will leave the new Date blank to get the time difference between UTC and local time by a trigger.
HTML
<!DOCTYPE html> < html lang = "en" > < head > < title >JavaScript Date getTimezoneOffset() Method</ title > </ head > < body style = "text-align: center;" > < h1 style = "color: green;" >GeeksforGeeks</ h1 > < b >JavaScript Date geTimexoneOffset() Method</ b > < br /> < br /> < button onclick = "gfg()" >Click me</ button > < script > function gfg() { // If nothing is in parameter it takes // the current date while creating // Date object let date = new Date("June 29 2020 10:30"); // Extracting the time difference between // UTC and local time let diff = date.getTimezoneOffset(); //Printing on console document.write(diff); } </ script > </ body > </ html > |
Output:
Note: The Universal Coordinated Time (UTC) is the time set by the World Time Standard. UTC is the same as GMT time.
We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.
Supported Browsers: The browsers supported by JavaScript Date getTimezoneOffset() method are listed below:
- Google Chrome
- Internet Explorer
- Mozilla Firefox
- Opera
- Safari
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.
Please Login to comment...