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
source
share