fullybaked | davebaker

I'm a developer living in the UK and when I'm not spending time with my wife and our 2 wonderful little boys, I love hacking around with code

I'm passionate about technology, web development, gaming and fencing

5 October 2012

iOS 5 & Javascript Gotcha

Just a short post this time. I came across this little gotcha when working on an offline iOS app using HTML5 Cache Manifest and Javascript

var date = new Date('2012-10-05');

This sets up a Javascript date object perfect in every browser I've used in quite a while. I used it today in my offline app (testing in iOS6) and it continued to work just fine.


After dropping the app into test, one of our testers pointed out that a particular view wasn't working properly. Instead of filtering a list of events by date, it was showing everything!

As it turned out, iOS5 was returning the string

'Invalid Date'

for the above piece of code. Instead of parsing the date string and returning a date object it was failing.

To fix this, I had to split the date string on the hyphen then build the date object using the alternative syntax

var parts = '2012-10-05'.split('-');
var date = new Date(parts[0], parts[1]-1, parts[2]);

Remember, when doing it this way to subtract 1 from the month as the Javascript date object is zero-indexed for months.