skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. Due to doc https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

    date.getTimezoneOffset() returns the difference, in minutes, between date as evaluated in the UTC time zone and as evaluated in the local time zone — that is, the time zone of the host system in which the browser is being used (if the code is run from the Web in a browser), or otherwise the host system of whatever JavaScript runtime (for example, a Node.js environment) the code is executed in.

    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

    let a = new Date();
    console.log('Today:', a.getTimezoneOffset());
    // Today: -180
    
    let b = new Date('2024-03-30');
    console.log('Yesterday:', b.getTimezoneOffset());
    // Yesterday: -120
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search