背景参数存在刚刚有其属性的存在……

在开辟过程当中ajax要求对获得的额数据举行操纵 以下:

var strTimeStr=data.startTime;
var newstartTime=startTime=new Date(Date.parse(strTimeStr.replace(/-/g,"/"))).getTime(); 
……

再自测时发明报错,以至于背面的代码都住手实行。
报错:js提醒 Cannot read property ‘replace’ of undefined

原因是在某些情况下返回的data中没有参数“startTime” 故“replace”不存在。

革新:

 if(data.startTime){  //当data.startTime存在时
                    var strTimeStr=data.startTime;   //背景返回的额时候是“2016-12-12 00:00:00”花样的字符串
                    var newstartTime=startTime=new Date(Date.parse(strTimeStr.replace(/-/g,"/"))).getTime(); //背景返回的string时候转为时候戳
                }else{
                    console.log("startTime:不存在");   
                };

要用这类转换 var newstartTime=startTime=new Date(Date.parse(strTimeStr.replace(/-/g,”/”))).getTime(); 时候花样为“2016-12-12 00:00:00”

IOS剖析Date.parse(“Mon Dec 12 2016 10:00:00“).getTime()如许的时候花样时 ios报NaN

所以背景返回时候花样只管为时候花样为“2016-12-12 00:00:00

如若笔记有误,望指出,非常感谢~

    原文作者:小五_fiona
    原文地址: https://segmentfault.com/a/1190000007753225
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞