Can someone explain why the following snippets result in an invalid date object?
new Date(new Date().toLocaleString())
// or
Date.parse(new Date().toLocaleString())
Can someone explain why the following snippets result in an invalid date object?
new Date(new Date().toLocaleString())
// or
Date.parse(new Date().toLocaleString())
This is expressly permitted by the ES5 specification's definition of
Date.parse
(emphasis mine):Since
toLocaleString
is not required to produce a string conformant to the Date Time String FormatYYYY-MM-DDTHH:mm:ss.sssZ
, it is allowable for its output not to be parsed correctly byDate.parse
.new Date().toLocaleString()
returns the current date in a formatnew Date()
can't parse, resulting in unexpected dates.