dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

GMT 0 timezones don't work when locale is set

Open justingolden21 opened this issue 1 year ago • 4 comments

Describe the bug

GMT 0 timezones don't work when locale is set

dayjs($now).tz('Etc/GMT').locale( 'en').format('h:mm A')

INCORRECT time: 1:36pm

dayjs($now).tz('Etc/GMT').format('h:mm A') (without locale)

CORRECT time: 5:36pm

This bug is only present if the timezone is 'Etc/GMT' (or 'Europe/London') and works fine in 'Etc/GMT+1' and 'Etc/GMT-1' with locale set.

Expected behavior

The correct time should be shown when tz is set to GMT and locale is set to en

Information

  • Dayjs 1.11.10
  • Windows 10
  • Chrome Version 120.0.6099.130 (Official Build) (64-bit)
  • Time zone: Etc/GMT (aka Europe/London)

justingolden21 avatar Jan 05 '24 05:01 justingolden21

My friend found that while the following line of dayjs doesn't work:

dayjs($now).tz('Etc/GMT').locale('en').format('h:mm A');

We can do it using vanilla JS with intl:

Intl.DateTimeFormat('en', {
  timeZone: 'Etc/GMT',
  hour: 'numeric',
  minute: 'numeric'
}).format($now);

However, I use dayjs throughout the whole project (and enjoy using it), and I've already based the app's time formatting off of the dayjs system.

Maybe someone else can benefit from this info though

justingolden21 avatar Jan 06 '24 01:01 justingolden21

I have the same issue and I cannot use Intl as my dependencies offer dayjs adapters internally

dominikbrazdil avatar Mar 21 '24 06:03 dominikbrazdil

I believe I'm experiencing a related issue.

When setting the hour of a GMT Day.js object, the timezone of the object seems to get shifted back 4 hours (which maps to my offset from GMT, I am in NYC). This does not happen for Day.js objects with a different timezone.

The only way I am able to stop this behavior is if set my system time to GMT.

const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')

dayjs.extend(utc)
dayjs.extend(timezone)

const gmtDate = dayjs.tz(1711584060000, "Etc/GMT")
const chicagoDate = dayjs.tz(1711584060000, "America/Chicago")

// When system time is EDT
console.log(gmtDate.toString()) // Thu, 28 Mar 2024 00:01:00 GMT
console.log(gmtDate.hour(gmtDate.hour()).toString()) // Wed, 27 Mar 2024 20:01:00 GMT
console.log(chicagoDate.toString()) // Thu, 28 Mar 2024 00:01:00 GMT
console.log(chicagoDate.hour(chicagoDate.hour()).toString()) // Thu, 28 Mar 2024 00:01:00 GMT

// When system time is GMT, EVERY result is Thu, 28 Mar 2024 00:01:00 GMT

harrydshapiro avatar Apr 01 '24 21:04 harrydshapiro

@justingolden21 @dominikbrazdil I have been able to get my code to work by downgrading to version 1.9.4 - anything after that experiences the bug. Perhaps the same would work for you?

harrydshapiro avatar Apr 01 '24 21:04 harrydshapiro