r/javascript • u/robertgambee • Mar 18 '26
`new Date()` considered harmful
https://futuresearch.ai/blog/javascript-thinks-everythings-a-date/
0
Upvotes
r/javascript • u/robertgambee • Mar 18 '26
View all comments
0
u/robertgambee Mar 18 '26
I recently spent an afternoon learning that JavaScript has a very generous definition of "date."
Makes sense. ISO format, midnight UTC, so it shows up as January 22 in the Western Hemisphere.
OK, it pulled the date out of a sentence, which might be helpful in some cases. And interestingly, the time shifted, which is a little odd.
It thinks "Route 66" is referring to the year 1966? That's definitely a stretch.
Year 90,210? Are you kidding me?!
Turns out that most popular JavaScript engines have legacy parsers that really, really want to help you parse dates.
We had a bug in our app were addresses and business names were being displayed as dates. The reason was that we were using the
Dateconstructor as a fallback parser to catch unexpected formats. The fix was simple, but the bug made us laugh when we first saw it. And we learned to not treat theDateconstructor as a validator.