new Date(time).getTime()在ios返回NaN

 日期转成时间戳:

var time = "2017-06-23 17:00:00";
alert(new Date(time).getTime());

 

Android中可以正常返回,IOS返回NAN:

 

new Date(time).getTime()在ios返回NaN_第1张图片

 

 

返回日期对象时Android正常返回,IOS返回Invalid Date:

var time = "2017-06-23 17:00:00";
alert(new Date(time));

 IOS:

 new Date(time).getTime()在ios返回NaN_第2张图片

 

 解决办法:IOS做单独判断

判断是手机设备是IOS还是Android

IsAppleStore: function (phone) {
        var  u  =  navigator.userAgent;
        var  app  =  navigator.appVersion;        
        var  ios  =  !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);        
        var  iPad  =  u.indexOf('iPad')  >  -1;        
        var  iPhone  =  u.indexOf('iPhone')  >  -1  ||  u.indexOf('Mac')  >  -1;        
        if  (ios  ||  iPad  ||  iPhone)  {            
            return  true;        
        } 
        else  {            
            return  false;        
        }
},

 

 IOS识别

new Date(2010,0,1,0,0,0).getTime()

 

IOS与Android都支持写法

var time = "2017-06-23 17:00:00";
 time = time.replace(/-/g,':').replace(' ',':'); //2017:06:23:17:00:00
time = time.split(':');  //["2017", "06", "23", "17", "00", "00"]
//var time1 = new Date(2017,5,23,17,00,00);
var time1 = new Date(time[0],(time[1]-1),time[2],time[3],time[4],time[5]);
console.log(time1)         //Fri Jun 23 2017 17:00:00 GMT+0800 (中国标准时间),
console.log(typeof time1)  //object
alert(time1.getTime());    //1498208400000

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(new Date(time).getTime()在ios返回NaN)