Skip to content
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

Invalid date when changing timezone #2137

Closed
ablanco opened this issue Nov 23, 2022 · 2 comments
Closed

Invalid date when changing timezone #2137

ablanco opened this issue Nov 23, 2022 · 2 comments

Comments

@ablanco
Copy link

ablanco commented Nov 23, 2022

Describe the bug
I'm getting an Invalid Date error when changing the timezone with dayjs.

It is related to the src/plugin/timezone/index.js file, line 98 and 99.

const target = date.toLocaleString('en-US', { timeZone: timezone })
const diff = Math.round((date - new Date(target)) / 1000 / 60)

new Date doesn't work with the output of toLocaleString, so that conversion doesn't work. For example:

const date = Date(2022, 3, 20);
date.toLocaleString("en-US", { timeZone: "Europe/Madrid" });
// This outputs: '4/20/2022, 12:00:00 AM'
new Date('4/20/2022, 12:00:00 AM');
// This returns an Invalid Date

This makes this code to return an Invalid Date:

dayjs(`${date} ${time}`).tz('Europe/Madrid').format('MMM D, YYYY - HH:mm');

Expected behavior
I expected the method tz to return a valid dayjs object in the requested timezone.

Information

  • Day.js Version 1.11.6
  • OS: ArchLinux
  • Browser Chrome 107 (also in Node 19.0.1)
  • Time zone: GMT+01:00 CEST
@MZPL
Copy link

MZPL commented Nov 23, 2022

The issue has already been reported here: #2102

@ablanco
Copy link
Author

ablanco commented Nov 24, 2022

I looked for a similar bug, but I missed that one. Thanks!

@ablanco ablanco closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants