开发者

Javascript data parsing in IE vs other browsers... It seems kinda screwed up. What's the deal?

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.

A开发者_开发知识库m I missing something?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜