You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The function first attempts to parse the String according to the format described in Date Time String Format (21.4.1.18), including expanded years. If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.
and:
However, the expression
Date.parse(x.toLocaleString())
is not required to produce the same Number value as the preceding three expressions and, in general, the value produced by this function is implementation-defined when given any String value that does not conform to the Date Time String Format (21.4.1.18)
It does seem more of a coincidence that Date.parse(d.toLocaleString("en-US")) works, rather than a guarantee. But given it does work, and v8 specifically intended to delegate to icu for determining whitespace, it seems reasonable to continue the status quo.
In general though, if you want guaranteed-to-work round-trip date formatting and parsing, then you would want to use the ISO 8601 format given by toISOString().
It was never an issue in official 19.0.x releases. It was broken in 19.1.0 when Node switched to ICU 72. It is subsequently fixed in 19.2.0 which cherry-picked the v8 commit that fixes it.
The original report here of it breaking against 19.0.0 must have been linking against an external ICU which was ICU 72.
@srl295 indeed, but I don't know a better way now. A very popular library like dayjs uses it. Will be good to provide a better solution and add it to stack overflow.
I know it's not directly a nodejs issue but because it's so popular will be better to do something.
Activity
richardlau commentedon Oct 25, 2022
NARROW NO-BREAK SPACE sounds like it's from the CLDR 42 change in ICU 72.1
https://icu.unicode.org/download/72
except Node.js 19.0.0 doesn't ship with ICU 72.1 as that only landed on
maintoday (#45068). Did you build Node.js yourself?targos commentedon Oct 25, 2022
Another possibility is that you are using a Node.js linked against your system version of ICU, and that version was updated to 72.1.
dharesign commentedon Nov 15, 2022
Looks like a bug in v8. It's set up to handle unicode whitespace, but instead treats them as keywords. I created https://bugs.chromium.org/p/v8/issues/detail?id=13490
srl295 commentedon Nov 15, 2022
seems like user error not a bug, don't assume that Localized date format is parseable by Date.
Looking at the v8 issue, I can see where there is a coding bug on the V8 side, and I haven't looked at the spec yet.
But that said, I still disagree that the original example code here should always pass… It makes an invalid assumption about the US date format
Do not use a localized date format if you expect it to be machine readable!
dharesign commentedon Nov 15, 2022
The spec is here: https://tc39.es/ecma262/#sec-date.parse
It specifically says:
and:
I don't know if there's a more canonical reference for what implementation-defined strings v8 supports other than this comment in its source:
https://github.com/v8/v8/blob/5fe0aa3bc79c0a9d3ad546b79211f07105f09585/src/date/dateparser-inl.h#L23-L69
It does seem more of a coincidence that
Date.parse(d.toLocaleString("en-US"))works, rather than a guarantee. But given it does work, and v8 specifically intended to delegate to icu for determining whitespace, it seems reasonable to continue the status quo.In general though, if you want guaranteed-to-work round-trip date formatting and parsing, then you would want to use the ISO 8601 format given by
toISOString().dharesign commentedon Nov 21, 2022
This was fixed upstream:
https://bugs.chromium.org/p/v8/issues/detail?id=13494
srl295 commentedon Nov 21, 2022
FWIW change LGTM
tukusejssirs commentedon Nov 23, 2022
I just want to leave a note that
new Date(new Date().toLocaleString('en-US'))works as expected intnode@19.0.1, but not innode@19.1.0.deps: V8: cherry-pick 2ada52cffbff
deps: V8: cherry-pick 2ada52cffbff
deps: V8: cherry-pick 2ada52cffbff
deps: V8: cherry-pick 2ada52cffbff
Hexagon commentedon Nov 27, 2022
This is still a problem in 19.1
michelpromonet commentedon Nov 29, 2022
Hi,
It seems working on 19.0
gives
But not on latest/19.1
fails with
Then it seems fixed and re-broken ?
dharesign commentedon Nov 30, 2022
It was never an issue in official 19.0.x releases. It was broken in 19.1.0 when Node switched to ICU 72. It is subsequently fixed in 19.2.0 which cherry-picked the v8 commit that fixes it.
The original report here of it breaking against 19.0.0 must have been linking against an external ICU which was ICU 72.
stalkerg commentedon Dec 28, 2022
Unfortunately, it's a default way to change the timezone for the date https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript and many libraries use it.
Do you have a better way?
srl295 commentedon Dec 28, 2022
stalkerg commentedon Dec 29, 2022
@srl295 indeed, but I don't know a better way now. A very popular library like dayjs uses it. Will be good to provide a better solution and add it to stack overflow.
I know it's not directly a nodejs issue but because it's so popular will be better to do something.
deps: V8: cherry-pick 2ada52cffbff
fix: disalbe node v18
fix: support type inference of Translation, NumberFormat and Datetime…
hacky fix for https://github.com/nodejs/node/issues/45171\#issuecomme…
Clean up build pipelines (#4995)