New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dayjs.tz()
is building dates with incorrect timezone for many cases
#1827
Comments
I was about to file a bug regarding Example:
It seems to be applying the offset of the target timezone without taking into consideration the timezone of the original date! |
Let me contribute my tests:
This also touches #1816. |
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. |
dayjs.tz()
is not building dates with correct timezone in many casesdayjs.tz()
is not building dates with correct timezone for many cases
I am adding my bug here as well instead of creating a new one. Same nodejs environment: nodejs14.x use the same exact date and calling timezone with true as second parameter adjusts the time to utc offset
As you can see utc offset is applied if timezone is called twice. This is a bug and likely related to new nodejs environemnt possibly? not sure at this point. |
To date, I get the same result |
I've got the same. In my case, release 1.11.2 solved my problem. |
I commented above here, and the behavior I described above is also fixed in 1.11.2. |
My case seems also to be fixed. I tested it on the Day.js website in the console, so I don't know exactly in which version this has been fixed. |
dayjs(2022-11-19).tz('America/Los_angeles') => "2022-11-18T16:00:00.000Z" On my local it is correct. What can I do? Do I just need to use moment? I love this library but I am not sure if there is a workaround or not for timezones not working. I have had other issues with timezone as well but found workarounds. dayjs: 1.11.5 |
dayjs('2022-11-19 15:45:55 UTC').tz('America/Los_angeles') This fixed it for me and Idk why. Something to look into. |
On version 1.11.7, specifying timezone has no effect. I'm only getting the local time. |
This is a sandbox that reproduces the timezone not affecting the date: |
Recommend this over dayjs if you want to handle timezone: https://www.npmjs.com/package/date-fns-tz |
@pencilcheck date-fns-tz is a really pain :/ |
dayjs().tz('Asia/Jakarta').format(); use format(), this is working for me |
dayjs.tz()
is not building dates with correct timezone for many casesdayjs.tz()
is building dates with incorrect timezone for many cases
It seems that dayjs can not handle the timezone with the format of Here is my test case: const originalDate = dayjs('2024-01-18T12:34:56');
// GMT-8 is actually GMT+8, 2024-01-18 12:34:56
console.log(originalDate.utc().tz('Etc/GMT-8').format('YYYY-MM-DD HH:mm:ss'));
// UTC standard time, correct
console.log(originalDate.utc().tz('Etc/GMT+0').format('YYYY-MM-DD HH:mm:ss'));
// 2024-01-17 20:34:56, I have no idea what is going on here
console.log(originalDate.utc().tz('Etc/GMT+8').format('YYYY-MM-DD HH:mm:ss')); |
It turns out that the For example, the 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. |
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 :
2022-03-11T14:29:26.319Z
Fri, 11 Mar 2022 14:29:26 GMT
3/11/2022, 2:29:26 PM
2014-02-03T16:50:21Z
2012-02-01T13:50:21.01-03:00
2022-02-03T13:50:21-00: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
The text was updated successfully, but these errors were encountered: