mark
mark

Reputation: 62846

A normalized javascript implementation of the Date type?

new Date("2") is:

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).

Upvotes: 3

Views: 3677

Answers (1)

David M
David M

Reputation: 72900

No. The ECMAScript specification states that the single parameter Date constructor, where that parameter is a string, will defer to Date.parse which 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 the 2 to a more explicit constructor for Date.

Upvotes: 4

Related Questions