`dayjs.tz()` is not building dates with correct timezone for many cases
See original GitHub issueDescribe 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)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:11
Top Results From Across the Web
dayjs is not converting timezones properly - Stack Overflow
I fixed using utc first and then format on local timezone import dayjs ... const timeZone = dayjs.tz.guess() dayjs.utc(utcDate).tz(timeZone).
Read more >Time Zone
Day.js supports time zone via the Internationalization API in supported environments. By using the native API, no extra bytes of timezone data need...
Read more >Demystifying DateTime Manipulation in JavaScript - Toptal
The Date object's getTimeZoneOffset() function tells us the number of minutes that when added to a given local time gives the equivalent UTC...
Read more >iamkun/dayjs - Gitter
I am having some timezone issues....I have some date/time objects that I have to convert from UTC time to EST. I need to...
Read more >The best JavaScript date libraries in 2021 - Skypack Blog
For basic handling of dates, Day.js is a minimalist library that offers an excellent ... Why not just use JavaScript's built-in Date object?...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I was about to file a bug regarding
dayjs.tzand 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!
I’ve got the same.
In my case, release 1.11.2 solved my problem.