The getTimezoneOffset() method
returns the time difference between UTC time and local time in minutes.
Coordinated Universal Time (UTC) is the time standard set by the world, and UTC time is the same as Greenwich Mean Time (GMT).
Check the time difference between my time zone and the standard time zone.
function myDate () {
var d = new Date();
var n = d.getTimezoneOffset();
console.log(n)
}
myDate();
// PS E:\demo> node 1.js
// -480
That is 480 minutes (8 hours). Beijing time is in the UTC+8 time zone, which is exactly 8 hours ahead of Greenwich Mean Time. This indicates that the getTimezoneOffset()
method is effective.
Next, use this method to determine whether the current time zone is daylight saving time.
const time1 = new Date(2021, 0, 1);
const time2 = new Date(2021, 6, 1);
if (time1.getTimezoneOffset() != time2.getTimezoneOffset()) {
console.log('Daylight saving time');
}
else {
console.log('Not daylight saving time');
}
// PS E:\demo> node 1.js
// Not daylight saving time