今天测试微信小程序的时候发现日期在iOS微信上显示为null,而开发工具和安卓是正常的。
最后发现是创建Date对象出了问题:
var createTime = "2017-08-10 11:32:11"
var time = new Date()
console.log(createTime)
console.log(time) //iOS: null, 开发工具和安卓正常
解决办法:
// 1111-11-11 11:11:11
function dateFromString(time){
var time = "2014-08-12 09:25:24 "
time = time.replace(/-/g, ':').replace(' ', ':')
time = time.split(':')
var time1 = new Date(time[0], (time[1] - 1), time[2], time[3], time[4], time[5])
return time1
}
var createTime = "2017-08-10 11:32:11"
var time = dateFromString(createTime)
console.log(createTime)
console.log(time) //正常
方法来源:
javascript中new Date(****)结果为invalid date或为null解决方法
参考:
How to create a date object from string in javascript [duplicate]
网友评论