new Date("2") is:
- “Thu Feb 01 2001 00:00:00 GMT-0500 (Eastern Standard Time)” on Chrome
- “Invalid Date” on Firefox and IE 9
- “Thu, 01 Feb 2001 05:00:00 GMT” on node.js
Since node.js uses the google V8 javascript engine and Chrome is also from google, I suppose this is a googlish tweak.
This is bad. Is there an easy way to normalize the behavior of the Date type across the different platforms?
Thanks.
EDIT
Of course, “2” is not good for a date. But if one uses the “date” method of the jQuery validator plugin, then “2” is a perfectly valid input, because this particular validation method defers its logic to the Date javascript type to make the actual validation. Which makes perfect sense, if the Date type implementation is sensible. Which is apparently not the case in Chrome (and node.js).
No. The ECMAScript specification states that the single parameter
Dateconstructor, where that parameter is a string, will defer toDate.parsewhich is “implementation dependent”. Source: ECMAScript specification.Avoid using this constructor therefore if you want the same behaviour across implementations.
If you have, for your application, a recognised meaning of the string
"2"as a date, then you should implement your own logic to interpret it. There is no “standard” meaning for this. If you had something more recognisable to the world at large as your string, you’d find the different implementations behaved more similarly. But in your case you’d be advised to parse the string yourself and explicitly provide the meaning of the2to a more explicit constructor forDate.