Javascript date parsing on Iphone Javascript date parsing on Iphone javascript javascript

Javascript date parsing on Iphone


Not all browsers support the same date formats. The best approach is to split the string on the separator characters (-,   and :) instead, and pass each of the resulting array items to the Date constructor:

var arr = "2010-03-15 10:30:00".split(/[- :]/),    date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);console.log(date);//-> Mon Mar 15 2010 10:30:00 GMT+0000 (GMT Standard Time)

This will work the same in all browsers.


For UTC/GMT time, you can try:

    var arr = "2014-10-27T16:05:44+0000".split(/[\-\+ :T]/);    var date = new Date();    date.setUTCFullYear(arr[0]);    date.setUTCMonth(arr[1] - 1);    date.setUTCDate(arr[2]);    date.setUTCHours(arr[3]);    date.setUTCMinutes(arr[4]);    date.setUTCSeconds(arr[5]);

The date object will display in the proper local timezone when used.


You might have better luck if you stick to ISO 8601 format:

Date.parse("2010-03-15T10:30:00");// e.g.var d = new Date( Date.parse("2010-03-15T10:30:00") );console.log( d.toString() ); //Mon Mar 15 2010 10:30:00 GMT+0000 (BST)