getTimezoneOffset()
returns wrong timezone.
I’m in Kyiv and my local time is 10:07.
Kyiv time confirmed:
> new Date().toLocaleString('en-GB', {timeZone: 'Europe/Kyiv' });
// '31/03/2024, 10:07:28'
Yesterday Kyiv was on winter time GMT+3, but today winter time is off and Kyiv is on GMT+2.
2 hour difference confirmed:
> new Date().toLocaleString('en-GB', {timeZone: 'Europe/London' });
// '31/03/2024, 08:07:07'
So, getTimezoneOffset() suppose to return -120, right? No.
> new Date().getTimezoneOffset()
// -180
I switched local TZ on my PC to Paris and got -120
though Paris is GMT+1 and -60
was expected. So it seems getTimezoneOffset()
is one hour off for all the TZs.
I can see that people struggled with getTimezoneOffset()
before and some workarounds are out there, but the question stands: Why getTimezoneOffset()
returns wrong result at the first place?
2
Answers
Date().getTimezoneOffset() does not give you the offset when compared to Europe/London. It gives you the offset compared to UTC.
Europe/London, at the time you asked the question, is not the same as UTC.
Kyiv may well be 2 hours away from London but it is 3 hours away from UTC hence 180 being the correct result.
Due to doc https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
So Europe/Kyiv was UTC+2 (-120 minutes), so called winter time. 31.03.2024 there going forward for an one hour (3am -> 4am). It became UTC+3 (-180 minutes), so called summer time.
You can run this snippet in your browser, and if your local timezone is Europe/Kyiv you can see the same results, as in comments