question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`dayjs.tz()` is not building dates with correct timezone for many cases

See original GitHub issue

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)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:11

github_iconTop GitHub Comments

4reactions
hbjcommented, Mar 28, 2022

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:

dayjs.tz('2022-02-21 22:00', 'YYYY-MM-DD HH:mm', 'Europe/Paris').toISOString()
=> '2022-02-21T21:00:00.000Z'
dayjs.tz('2022-02-21 22:00', 'YYYY-MM-DD HH:mm', 'Europe/Paris').tz('Europe/Paris').toISOString()
=> '2022-02-21T22:00:00.000Z'

It seems to be applying the offset of the target timezone without taking into consideration the timezone of the original date!

2reactions
sevraicommented, Aug 22, 2022

To date, I get the same result 2022-02-21T21:00:00.000Z in both cases dayjs: 1.11.5 macOS NodeJS: 16.13.1

I’ve got the same.

In my case, release 1.11.2 solved my problem.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found