在开发过程当中ajax请求对获得的额数据进行操做 以下:ios
var strTimeStr=data.startTime; var newstartTime=startTime=new Date(Date.parse(strTimeStr.replace(/-/g,"/"))).getTime(); ……
再自测时发现报错,以致于后面的代码都中止执行。
报错:js提示 Cannot read property 'replace' of undefinedajax
缘由是在某些状况下返回的data中没有参数“startTime” 故“replace”不存在。code
改进:开发
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;get
因此后台返回时间格式尽可能为时间格式为“2016-12-12 00:00:00”string
如若笔记有误,望指出,很是感谢~io