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("2013-11-18T11:55:20").tz("America/Toronto")` does not work

See original GitHub issue

Describe the bug dayjs("2013-11-18T11:55:20").tz("America/Toronto") (as suggested in the docs) outputs the following:

const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
dayjs.extend(utc)
dayjs.extend(timezone)

console.log(dayjs('2013-11-18T11:55:20').tz('America/Toronto'))

// Output
{
  '$L': 'en',
  '$d': Invalid Date,
  '$x': { '$timezone': 'America/Toronto' },
  '$y': NaN,
  '$M': NaN,
  '$D': NaN,
  '$W': NaN,
  '$H': NaN,
  '$m': NaN,
  '$s': NaN,
  '$ms': NaN,
  '$offset': NaN,
  '$u': false
}

Expected behavior

The following should be the output:

{
  '$L': 'en',
  '$d': 2013-11-18T10:55:20.000Z,
  '$x': { '$localOffset': -60, '$timezone': 'America/Toronto' },
  '$y': 2013,
  '$M': 10,
  '$D': 18,
  '$W': 1,
  '$H': 11,
  '$m': 55,
  '$s': 20,
  '$ms': 0,
  '$offset': -300
}

Information

  • Day.js Version: 1.11.6, 1.11.0, 1.10.0, 1.9.0 (it seems like the bug is in all versions since utc plugin was introduced)
  • OS: [e.g. iOS]: Arch Linux
  • Browser [e.g. chrome 62]: n/a
  • Time zone: [e.g. GMT-07:00 DST (Pacific Daylight Time)]: tested with America/Toronto and Europe/Slovakia; I don’t think it matters;
  • Node version: 19.0.0
  • NPM version: 8.19.2

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
galenhuntingtoncommented, Nov 18, 2022

The problem appears to be here: https://github.com/iamkun/dayjs/blob/9c20e77caf7b1b5eccf418175203b198d4e29535/src/plugin/timezone/index.js#L98-L99

The offset is calculated by converting a Date to an en-US locale string, then back again to a Date. However, the conversion back fails in some environments:

> new Date().toLocaleString('en-US', { timeZone: "America/Boise" })
'11/18/2022, 1:27:14 PM'
> new Date('11/18/2022, 1:27:14 PM')
Invalid Date

This may depend on obscure details of the OS you’re running on.

For some other locales this works without issue:

> new Date().toLocaleString('zh', { timeZone: "America/Boise" })
'2022/11/18 13:28:30'
> new Date('2022/11/18 13:28:30')
2022-11-18T13:28:30.000Z
Read more comments on GitHub >

github_iconTop Results From Across the Web

`dayjs.tz()` is not building dates with correct timezone for many ...
The issue is that the function is not converting correctly between timezones. Simplest case to see is when converting to the same 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 >
dayjs timeZone is not supported - react native - Stack Overflow
I have tried to update the dayjs package,the intl-react package and intl package itself, couldn't found any solution to this problem with react ......
Read more >
Working with Dates and Times with Day.js
To learn more about JavaScript dates, kindly visit this link. Formatting dates. Get the formatted date using the token string that was supplied....
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