views:

70

answers:

2

Firstly, when I say other browsers I really only mean Firefox because that's all I tested in.

Internet Explorer can parse a date followed by a single character as a proper date. Whereas Firefox behaves as I'd expect.

For example...

var dateString = new Date("1/1/2010f");
alert(dateString);

In IE it will alert...

Thu Dec 31 21:00:00 UTC-0900 2009

Whereas in FF is will spit out...

"Invalid Date"

I first noticed this using the jquery validation plug in.

http://docs.jquery.com/Plugins/Validation/Methods/date

It seems like it just subtracts some amount of hours off the actual date in IE when a character is appended. I've tested in IE6 and IE8.

Am I missing something?

A: 

Internet Explorer uses a different parsing mechanism, which may or may not be documented, and may be very broken in some or many cases.

lunixbochs
wtf. That's all there is to it eh? I would have expected this from IE6, but IE8 not so much.
Carter
+2  A: 

It is implementation dependent. When Date as a "constructor" is passed a string object, it tries to build a date with its parse method (Date.parse). That method shall parse strings returned by Date.toString() and Date.toUTCString(), which both return formats varying with the implementation. And this is how the ECMAScript standard says it should be.
I remember reading this in "the Rhino book" (Tommy Flanagan's great javascript book).

In practice, it might very well turn out everyone except IE does it the same way, but I haven't tested thoroughly.
Also, it is a bit much if IE tries to parse whatever one throws at it and produces a Date object out of ascii soup.

npup
Thank ya. It may be worth noting that it can't parse everything. 1/1/2010ff can't be parsed for example. It's a bummer because it means that the jquery validation plug-in won't cover the bases for IE because all it really does to validate dates is it creates a new Date object with the date string as a parameter.
Carter