iOS Date Parsing Issue

The following code works in web browsers but returns "Invalid Date" on iOS 5. I noticed while building a PhoneGap based app.

// This returns invalid date in iOS.
var yearMonth = "2012-08";
var date = new Date(yearMonth);
document.write(date);

Solution

Split the string into parts and pass in the first two items of the array. Since January is 0 and December is 11, remember to take 1 from the month value. Should work.

// This works in iOS.
var yearMonth = "2012-08";
var dateParts = yearMonth.split("-");
var date = new Date(dateParts[0], dateParts[1] - 1);
document.write(date);

If anyone has a cleaner method to do this, let me know. Much appreciated.

Ooooh, ultrawide! 😍