There are many warnings out there about not using new Date(string)
(or the equivalent Date.parse(string)
in javascript because of browser inconsistencies. MDN has this to say:
It is not recommended to use Date.parse as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).
However when you read on, most of the warnings about implementation-specific behaviour seem to be for these scenarios:
- Old browsers (like, pre-ES5 old)
- Non-ISO 8601 inputs (e.g.
"March 6th 2015"
) - Incomplete ISO 8601 inputs (e.g.
"2015-06-03"
, without the time or timezone)
What I would like to know is, given these two assumptions:
- Modern browsers (say, anything from 2020 onwards)
- Full ISO 8601 inputs (e.g.
"2021-11-26T23:04:00.778Z"
)
Can I reliably use new Date(string)
?