Safari on iOS is probably one of the best mobile browsers. Still there is one thing that I often got wrong and wonder why my webapp doesn’t work.
Javascript Date Object in iOS Safari
In every normal browser you could just create a Date object from string as
var myDate = new Date('2013-01-21T13:46:20');
But on iOS this simply gives you a JS error.
To fix it you should use the good ol’ split() and do something like this
//First we split the date string
var iosDateArr = '2013-01-21T13:46:20'.split('[- :T]');
//And use the constructor with 6 args
cDate = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);
Note that the Month in the array is reduced by one. This is because the Months are counted from 0 for January to 11 for December.
Leave a Reply