colinramsay.co.uk

JavaScript Date Implementations & Mobile Safari

03 Oct 2011

While there's commonality between browser Date() implementations, there are edge cases where certain browsers fail when others pass. This is particularly frustrating when working with Mobile Safari because its debugging support is limited to say the least. Take this example:

var date = new Date('Sun Oct 10 10:41:43 UTC 2010');

This works just fine for me in Chrome, Safari, and even my current version of the iOS Simulator's Mobile Safari. However on my iPhone 4 with the iOS 5 beta, this code:

console.log(date);

Will simply give "Invalid Date". Because of Mobile Safari's poor error reporting for things like this it can be extremely difficult to notice that Date() is the ultimate issue. The solution is to use a fixed implementation of Date - I used Date.parseDate within Sencha Touch, but if you're not using a framework you could look at DateJS.