Is Date.getTimezoneOffset upside down?

I have a JSON response from my server that gives me UTC Unix timestamps in seconds. I make it out of the JavaScript dates that will be used in the chart (displaying the time in the user locale).

I obviously need to persuade the timestamp that I have (in UTC) in the browser locale, so I wrote a function that creates a new one Datein the browser locale, calls getTimezoneOffset()on it to get the "offset in minutes" in the current region, according MDN , converts both values ​​to milliseconds and returns the sum. Now I have a Unix timestamp for JavaScript in the user locale.

However, I do not.

As it turned out, it (new Date()).getTimezoneOffset()returns (positively) 300 in GMT-5 and -120 in GMT + 2. Why is the offset reversed? I expected the offset to match the time zone sign - that is: I need to subtract 300 minutes to get to GMT-5, and ADD 120 minutes to get to GMT + 2. Instead, I have to subtract the values ​​returnedgetTimezoneOffset

+5
source share
1 answer

Nope.

spec (Β§15.9.5.26) says:

15.9.5.26 Date.prototype.getTimezoneOffset ()

Returns the difference between local time and UTC time in minutes.

  • Let t be the value of time.
  • If t is NaN, return NaN.
  • Return (t - LocalTime (t)) / msPerMinute.
+5
source

All Articles