V8 Date parser is broken:
> new Date('asd qw 101')
Sat Jan 01 101 00:00:00 GMT+0100 (CET)
I can use fragile regular expression like this:
\d{1,2} (jan|feb|mar|may|jun|jul|aug|sep|oct|nov|dec) \d{1,4}
but it is too fragile. I cannot rely on new Date
(issue in V8) and also moment cant help me because moment is getting rid off date detection (github issue-thread).
is there any workaround for broken v8 date parser?
To be clear. We have Gecko and V8, both have Date
. V8 has broken Date, Gecko has working one. I need the Date
from in Gecko (Firefox).
Update: It’s definitely broken parser https://code.google.com/p/v8/issues/detail?id=2602
nope, Status: WorkingAsIntended
Date
objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC and have the following constructorsFrom the docs,
Now looking at the v8 sourcecode in date.js:
it looks like
DateParse()
does not return a NaN for for a string like'asd qw 101'
and hence the error. You can cross-check the same withDate.parse('asd qw 101')
in both Chrome(v8) [which returns-58979943000000
] and Gecko (Firefox) [which returns a NaN].Sat Jan 01 101 00:00:00
comes when you seednew Date()
with a timestamp of -58979943000000(in both browsers)I wouldnt say V8 date parser is broken. It just tries to satisfy a string against RFC 2822 standard in the best possible way but so does gecko and both
breakgives different results in certain cases.Try
new Date('Sun Ma 10 2015')
in both Chrome(V8) and Firefox(Gecko) for another such anomaly. Here chrome cannot decide weather 'Ma' stands for 'March' or 'May' and gives an Invalid Date while Firefox doesnt.Workaround:
You can create your own wrapper around
Date()
to filter those strings that V8's own parser cannot. However, subclassing built-ins in ECMA-5 is not feasible. In ECMA-6, it will be possible to subclass built-in constructors (Array, Date, and Error) - referenceHowever you can use a more robust regular expression to validate strings against RFC 2822/ISO 8601
Image generated from debuggex
So, seems like v8 aint broken, it just works differently.
Hope it helps!