dayjs: `dayjs.tz()` is building dates with incorrect timezone for many cases
Describe the bug
When you try to use dayjs.tz
factory it isn’t providing the instance properly, even using some ISO formats and Date object string outputs.
Some examples using a UTC environment :
Input | Output | Expected |
---|---|---|
2022-03-11T14:29:26.319Z |
2022-03-11T14:29:26-03:00 | 2022-03-11T11:29:26-03:00 |
Fri, 11 Mar 2022 14:29:26 GMT |
2022-03-11T14:29:26-03:00 | 2022-03-11T11:29:26-03:00 |
3/11/2022, 2:29:26 PM |
2022-03-11T14:29:26-03:00 | 2022-03-11T11:29:26-03:00 |
2014-02-03T16:50:21Z |
2014-02-03T16:50:21-03:00 | 2014-02-03T13:50:21-03:00 |
2012-02-01T13:50:21.01-03:00 |
2012-02-01T16:50:21-03:00 | 2012-02-01T13:50:21-03:00 |
2022-02-03T13:50:21-00:00 |
2022-02-03T13:50:21-03:00 | 2022-02-03T10:50:21-03:00 |
The behavior is odd, for the Date input it fails sometimes but also should be accepted especially the ISO format native Date outputs.
Reproducible code:
https://gist.github.com/LeonanCarvalho/35d1596dcfb701255d04b93d70df69a0
Expected behavior Construct dayjs with correct timezone.
Information
- Day.js Version v1.10.8
- OS: [e.g. iOS]
- Browser: nodeJS v14.17.3
- Time zone: UTC (tests in UTC but also in GMT -3)
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 10
- Comments: 19
On version 1.11.7, specifying timezone has no effect. I’m only getting the local time.
I was about to file a bug regarding
dayjs.tz
and found this one which seems related. The issue is that the function is not converting correctly between timezones. Simplest case to see is when converting to the same timezone is leading to the time to be changed!Example:
It seems to be applying the offset of the target timezone without taking into consideration the timezone of the original date!
It turns out that the
Etc/
prefix would make the meaning of the whole timezone to be completely opposite than the original meaning.For example, the
Etc/GMT-8
is identical toGMT+8
, andEtc/GMT+2
is the same asGMT-2
alone.So the problem is not with dayjs, but my misunderstanding of the IANA timezone standard, although it’s a little counter intuitive.
Leaving a message here in case of people may run into the same problem like me.
I’ve got the same.
In my case, release 1.11.2 solved my problem.
This is a sandbox that reproduces the timezone not affecting the date: https://codesandbox.io/s/dayjs-business-time-forked-b7opew
It seems to be related to the extra offset created by Summer Time.
The switch to summer time occurred March 27th at 2AM in Paris and I got these results on an instance based in Paris:
It looks like the lib is using the current offset of the instance timezone (UTC+2 at the execution time) instead of the real one at this date.
Let me contribute my tests:
This also touches #1816.