dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Wrong time in GMT timezones when locale is set

Open justingolden21 opened this issue 3 years ago • 2 comments

Describe the bug

The following code:

new dayjs(newDate())
  .tz('Europe/London')
  .locale('en')
  .format('h:mm A');

Returns a time in EST (5 hours behind London, 3 hours ahead of me)

I'm located in California, not that that should matter here.

All other timezones that aren't in GMT time work perfectly fine

new dayjs(newDate())
  .tz('Europe/London')
  .format('h:mm A');

Not setting the locale this works perfectly fine.

Please assist

Expected behavior

The correct time in London

Information

  • Day.js Version ^1.10.7
  • OS: Windows
  • Browser: Chrome Version 97.0.4692.71 (Official Build) (64-bit)
  • Time zone: GMT-08:00 PST (Pacific Standard Time)

justingolden21 avatar Feb 23 '22 05:02 justingolden21

It's not off by 4 hours which is interesting. Haven't had daylight savings in either timezone since I reported the bug.

justingolden21 avatar Feb 25 '22 02:02 justingolden21

What a curious little bug you've found! For one, it is actually daylight savings sensitive. When you first reported the bug, the current date and time were in daylight savings time, which, in order to be reproducible, should be written like this:

> let bugexample = dayjs('2022-02-23T10:00:00-05:00');
> bugexample.tz('Europe/London').locale('en').format('h:mm A');
'8:00 PM'
> bugexample.tz('Europe/London').format('h:mm A');
'3:00 PM'

But if we use a date in standard time (like right now as of this writing), the anomaly disappears:

> let bugexample2 = dayjs('2022-06-14T10:00:00-05:00');
> bugexample2.tz('Europe/London').locale('en').format('h:mm A');
'4:00 PM'
> bugexample2.tz('Europe/London').format('h:mm A');
'4:00 PM'

As a note, invoking .utc() seems to be a workaround for the bug in the first case:

> bugexample.tz('Europe/London').utc().locale('en').format('h:mm A');
'3:00 PM'

Thanks for the report!

Juanc1to avatar Jun 15 '22 02:06 Juanc1to