dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

inconsistent timezone offset when passing in unix timestamp and iso string

Open yshwaker opened this issue 5 years ago • 7 comments

Describe the bug I am seeing different results when passing in unix timestamps and iso string. 1596265200000 is 2020-08-01 at that timezone.

dayjs.tz(1596265200000, 'America/Los_Angeles').format()
"2020-08-01T00:00:00-08:00"

dayjs.tz('2020-08-01', 'America/Los_Angeles').format()
"2020-08-01T00:00:00-07:00"

dayjs('2020-08-01').tz('America/Los_Angeles').format()
"2020-08-01T00:00:00-08:00"

dayjs('2020-08-01').tz('America/Los_Angeles').utcOffset()/60
-8

moment('2020-08-01').tz('America/Los_Angeles').utcOffset()/60
-7

Expected behavior should all be 2020-08-01T00:00:00-07:00, since America/Los_Angeles is UTC-07:00 in summer.

Information

  • Day.js Version: v1.9.6
  • OS: Mac
  • Browser: chrome 86
  • Time zone: GMT-08:00 PST (Pacific Standard Time)]

yshwaker avatar Nov 13 '20 20:11 yshwaker

Can you please try the latest version? This should be fixed already.

iamkun avatar Nov 14 '20 03:11 iamkun

@iamkun I was already testing on latest version which is 1.9.6. and I tested on the console of Dayjs page. the issue still happens in the latest version.

sandbox: https://codesandbox.io/s/serene-carson-ifzpu

yshwaker avatar Nov 14 '20 03:11 yshwaker

the tests also failed when running in America/Los_Angeles timezone or TZ=America/Los_Angeles jest test/plugin/timezone.test.js

yshwaker avatar Nov 14 '20 07:11 yshwaker

@iamkun as of version 1.10.1, the bug is still there and the result of the second one changed but still incorrect:

dayjs.tz(1596265200000, 'America/Los_Angeles').format()
"2020-08-01T00:00:00-08:00"

dayjs.tz('2020-08-01', 'America/Los_Angeles').format()
"2020-08-01T00:00:00Z"

dayjs('2020-08-01').tz('America/Los_Angeles').format()
"2020-08-01T00:00:00-08:00"

dayjs('2020-08-01').tz('America/Los_Angeles').utcOffset()/60
-8

moment('2020-08-01').tz('America/Los_Angeles').utcOffset()/60
-7

dayjs.tz('2020-08-01', 'America/Los_Angeles').utcOffset()/60
0

yshwaker avatar Jan 04 '21 23:01 yshwaker

This appears to be fixed in the latest version. All correctly return -7 when run on https://day.js.org

cschwebk avatar Sep 30 '21 17:09 cschwebk

Hi,

I am still noticing this issue with the latest version. When applying timezone plugin to dates that are in summer months, time shifts back 1 hour. Browser's timezone was GMT+0200 (EEST)

//December
dayjs.unix(1639998000).unix()
1639998000
dayjs.unix(1639998000).tz().unix()
1639998000 //Correct

dayjs.unix(1639998000).toISOString()
'2021-12-20T11:00:00.000Z'
dayjs.unix(1639998000).tz().toISOString()
'2021-12-20T11:00:00.000Z' //Correct

//June
dayjs.unix(1624446000).unix()
1624446000
dayjs.unix(1624446000).tz().unix()
1624442400 //Incorrect

dayjs.unix(1624446000).toISOString()
'2021-06-23T11:00:00.000Z'
dayjs.unix(1624446000).tz().toISOString()
'2021-06-23T10:00:00.000Z' //Incorrect

Expected behaviour The same result should be returned with or without timezone plugin being applied, since Unix time / UTC time is timezone independant.

girtsu avatar Jan 03 '22 11:01 girtsu

I still see this issue too. For this, I get two different results.

const date1 = dayjs(1744974180000).toDate().getTimezoneOffset();
const date2 = dayjs().toDate().getTimezoneOffset();
console.log(offset1); // 300
console.log(offset2); //360

Here is a StackBlitz

jaaywags avatar Mar 03 '25 03:03 jaaywags