JavaScript Date constructor wraparound broken in Safari 2

2007-02-13 05:21:00

This is a bug in Safari that some of our Cosmo tests turned up.

I had to do bit of digging around to figure out what was going on, so I thought I'd post it here and maybe save other people some trouble if they run into the same thing.

When instantiating a JavaScript Date with a month parameter higher than 11 or less than 0 (December and January, in the zero-based world of JavaScript Dates), the date should 'wrap around' to the correct month in the following/previous year (see ECMAScript 262 Third Edition, section, MakeDay).

Safari can't handle these out-of-range parameters properly, but doesn't even have the good sense to fail in those cases -- it just produces some seemingly random date. The setMonth method of Date unsurprisingly has the same issue. This problem is documented in WebKit bug 4892.

Unfortunately this is one of those "doctor it hurts when I do this" kind of problems -- the only practical solution is just not to expect intelligent wraparound from Dates in Safari.

Happily, the problem is fixed in the WebKit nightlies, so when Apple revs Safari, this should be fixed. Fortunately Safari users are a relatively small group, and very likely to upgrade, so hopefully this won't be a thorn in our side that much longer.


This is the blog for Matthew Eernisse. I currently work at Yammer as a developer, working mostly with JavaScript. All opinions expressed here are my own, not my employer's.


Previous posts

All previous posts ยป

This blog is a GeddyJS application.