开发者

javascript date works in all browsers except iPhone/iPod Touch

开发者 https://www.devze.com 2022-12-23 00:49 出处:网络
I\'ve got some code to work with dates in javascript. This works in IE, FF, Safari (desktop versions win & mac), Chrome, Opera.

I've got some code to work with dates in javascript. This works in IE, FF, Safari (desktop versions win & mac), Chrome, Opera. In iPhone safari (mobile safari), i get a 'invalid date' response.

The code for managing dates is

    function fixDateFormat(dateText){
    var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
        newDate = new Date(NaN), month,
        parts = isoExp.exec(dateText);

    if(parts) {
      month = +parts[2];
     开发者_开发知识库 newDate.setFullYear(parts[1], month - 1, parts[3]);
      if(month != newDate.getMonth() + 1) {
        newDate.setTime(NaN);
      } else {
        newDate.setHours(0, 0, 0, 0);
      }
    }
    return newDate;
    }

Where the dateFormat is sent into this function as Y-m-d (though it was my understanding that this function would deal with a multitude of formats).


There must be some for of bug in Mobile Safari, as I have this problem, it works everywhere apart from in an iOS device. It's not parsing a valid ISO datetime value correctly (eg '2011-10-09T14:00:00.0000000+01:00').

The problem with using the UNIX timestamp, although it works with the new Date().setTime() method; the time gets converted to UTC, so if your app doens't handle UTC offsets it'll show the wrong time. In particular, if the datetime refers to a date in the future, or over a period where daylight savings has changed, the time in a epoch timestamp will be incorrect. This is why timestamps aren't used to store datetime values.

The only workaround I could get to fix this was to pull apart the date object into a JSON object containing it's properties, and then reconstruct them on the client back into a Date() object.


I solved it by passing the 'milliseconds since epoch' instead of this formatted datestring.

0

精彩评论

暂无评论...
验证码 换一张
取 消