Home > Mobile >  Why getTimezoneOffset value depends on date?
Why getTimezoneOffset value depends on date?

Time:09-28

Why getTimezoneOffset() returns different timezone offset values based on provided date? I expect the results to be the same as i'm not changing my local timezone between each call.

new Date('2016-05-01T03:24:00Z').getTimezoneOffset()
> -120
new Date('1950-05-01T03:24:00Z').getTimezoneOffset();
> -60
new Date('1900-05-01T03:24:00Z').getTimezoneOffset();
> -84

CodePudding user response:

I think the official documentation points a few times for your question.

On "Varied results in Daylight Saving Time (DST) regions" section;

In a region that annually shifts in and out of Daylight Saving Time (DST), as date varies, the number of minutes returned by calling getTimezoneOffset() can be non-uniform.

Note: getTimezoneOffset()'s behavior will never differ based on the time when the code is run — its behavior is always consistent when running in the same region. Only the value of date affects the result.

On "getTimezoneOffset() and DST" secion;

In regions that use DST, the return value may change based on the time of the year date is in.

On "getTimezoneOffset() and historical data" section;

Due to historical reasons, the timezone a region is in can be constantly changing, even disregarding DST. For example, below is the output in a runtime in Shanghai, where the timezone is UTC 08:00.

const shModernOffset = new Date('2022-01-27').getTimezoneOffset(); // -480
const shHistoricalOffset = new Date('1943-01-27').getTimezoneOffset(); // -540

So, getting different results seems normal to me.

  • Related