Javascript date is invalid on iOS

前端 未结 7 796
忘掉有多难
忘掉有多难 2020-11-30 03:11

I\'m working on a Phonegap-based iOS app, which is already done for Android. The following lines are working fine for Android but not for iOS. Why?

var d = n         


        
相关标签:
7条回答
  • 2020-11-30 03:20

    Try with var d = new Date("2015/12/31 00:00:00"); Works for me.

    0 讨论(0)
  • 2020-11-30 03:21

    A solution that works for both IOS and Android, and avoid string manipulation when it isn't required is

    let fullDate = "1991-03-29 00:00:00";
    let date = new Date(fullDate);
    
    // In case its IOS, parse the fulldate parts and re-create the date object.
    if(Number.isNaN(date.getMonth())) {
      let arr = fullDate.split(/[- :]/);
      date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);
    }
    
    0 讨论(0)
  • 2020-11-30 03:22

    Your date string is not in a format specified to work with new Date. The only format in the spec is a simplified version of ISO-8601, but that was only added in ES5 and so support may be touch and go. Your string isn't in that format, but it's really close.

    If you change the space to a T, you'll be in spec:

    var dateString = "2015-12-31 00:00:00";
    var d = new Date(dateString.replace(' ', 'T'));
    

    (I'm assuming you're not actually using a string literal, hence the replace call.)

    Note that there was an error in the ES5 specification which was corrected in ES2015 (ES6): What happens when there's no timezone indicator on the string. In ISO-8601, no indicator means "local time," but the ES5 specification said that it defaults to Z (UTC —loosely, GMT). They fixed it in the ES2015 specification, but unfortunately some JavaScript engines followed the ES5 specification and others followed ISO-8601 (and now ES2015). But wait, it gets worse: Using local time for strings that just contained dates proved problematic for existing code (and TC39 really, really tries not to break existing code), so in ES2016 they had to change it again to say: If it's a date-only string, interpret it as UTC, but if it's a date/time string, interpret it as local time.

    So with all of that fun and games, for solid cross-browser support, you need to include a timezone indicator, because otherwise you don't know whether it will be interpreted as UTC or local time. You're allowed to use Z for GMT or +/- followed by HH:MM to give an offset. (Abbreviations like CST are not allowed, as there's no standard for them.)

    If they don't support that yet, there's near universal support for YYYY/MM/DD HH:MM:SS (interpreted as local time), even though it's not specified. So:

    var dateString = "2015-12-31 00:00:00";
    var d = new Date(dateString.replace(/-/g, '/'));
    
    0 讨论(0)
  • 2020-11-30 03:24

    I can't tell you why. Maybe because iOS doesn't support the Javascript Date function as well as Android, or support a different format?

    But I can give you a workaround:

    var s = "2015-12-31 00:00:00".split(" ")[0].split("-"),
        d = new Date( s[0], s[1], s[2], 0, 0, 0 );
    
    console.log(d);
    

    var s = "2015-12-31 00:00:00".replace(/[ :]/g, "-").split("-"),
        d = new Date( s[0], s[1], s[2], s[3], s[4], s[5] );
    
    console.log(d);
    
    0 讨论(0)
  • 2020-11-30 03:27

    I had such a hard time trying to solve this, i finally went for an easier solution and used moment.js. Not much you need to do just declare the dates and works in every device.

    0 讨论(0)
  • 2020-11-30 03:33

    If anybody is still looking for it.

    works for me in IE, Safari, IOS-FF, IOS-Safari ... etc.

    getIOSSaveDateObj = function(dateString){
        if(dateString.indexOf('-') > 0){
            var arr = dateString.split(/[- :]/);
            var date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]);
        }else{
            var arr = dateString.split(/[. :]/);
            var date = new Date(arr[2], arr[1]-1, arr[0], arr[3], arr[4], arr[5]);
        }
        return date;
    }
    
    0 讨论(0)
提交回复
热议问题