Skip to content

Invalid Date generated by toLocaleString('en-gb', ...) #42391

Open
@RobinBol

Description

@RobinBol

Version

v13.14.0

Platform

Darwin .... 21.3.0 Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_ARM64_T6000 arm64

Subsystem

Always on Node >= 13.

What steps will reproduce the bug?

nvm use 13
Now using node v13.14.0 (npm v6.14.4)
node -p "new Date().toLocaleString('en-GB', { timeZone: 'Europe/Amsterdam' })"
18/03/2022, 14:30:20
node -p "new Date(new Date().toLocaleString('en-GB', { timeZone: 'Europe/Amsterdam' }));"
Invalid Date

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

nvm use 12
Now using node v12.22.10 (npm v6.14.16)
node -p "new Date().toLocaleString('en-GB', { timeZone: 'Europe/Amsterdam' })"
3/18/2022, 2:29:41 PM
node -p "new Date(new Date().toLocaleString('en-GB', { timeZone: 'Europe/Amsterdam' }));"
2022-03-18T13:33:38.000Z

For the record, so far I have only found en-GB to have this problem, en-US works as expected, see below:

nvm use 13
Now using node v13.14.0 (npm v6.14.4)
node -p "new Date().toLocaleString('en-US', { timeZone: 'Europe/Amsterdam' })"
3/18/2022, 2:52:37 PM
node -p "new Date(new Date().toLocaleString('en-US', { timeZone: 'Europe/Amsterdam' }));"
2022-03-18T13:52:47.000Z

What do you see instead?

Invalid Date

Additional information

The day and month are swapped as of Node 13 for the en-GB locale.

For en-GB Node 13 seems to use the correct format day/month/year, whereas Node 12 uses month/day/year.

The problem is: in Node 12 you could create a Date instance based on the output of .toLocaleString('en-GB', ...). As of Node 13 this returns Invalid Date.

Node 12: the following results in a valid Date instance.

"new Date(new Date().toLocaleString('en-GB', { timeZone: 'Europe/Amsterdam' }));"

Node 13: the following results in an "Invalid Date" error.

"new Date(new Date().toLocaleString('en-GB', { timeZone: 'Europe/Amsterdam' }));"

Metadata

Metadata

Assignees

No one assigned

    Labels

    i18n-apiIssues and PRs related to the i18n implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions