The problem came with a string with the value "01 Jan 0001" which appeared randomly among the existing values. I first thought it was an error being thrown somewhere, or that it would not parse this string or even that it would be an overflow. It was none of that. Instead, it was about handling the year part.
A little context first:
Date.parse('01 Jan 0001') //978300000000 new Date(0) //Thu Jan 01 1970 00:00:00 Date.parse('01 Jan 1950') //-631159200000 new Date(Date.parse('01 Jan 1950')) //Sun Jan 01 1950 00:00:00 Date.parse('31 Dec 49 23:59:59.999') //2524600799999 Date.parse('1 Jan 50 00:00:00.000') //-631159200000 new Date(Date.parse('01 Jan 0001')) //Mon Jan 01 2001 00:00:00
The problem was with the handling of years that are numerically smaller than 50. The parser assumes we used a two digit year and translates it into Date.parse('01 Jan 01') which would be 2001. We get a glimpse into how it works, too, because everything between 50 and 99 would be translated into 19xx and everything between 00 and 49 is considered 20xx.
Note that .NET does not have this problem, correctly making the difference between a 2 digit and 4 digit year.
Hope it helps people.
Top comments (1)
I normally had to prevent parsing of numeric strings with
Also, don't trust user input strings or locale strings. Use ISO formats, or explicitly specify the format.