724 日 , 2021 19:29:15
new Date()时间格式化在IOS不兼容情况

在开发微信小程序时,需要对后端返回的时间进行计算,得用new Date()先格式化时间,在安卓和微信开发工具测试没问题,但IOS出错,原因是因为IOS系统不支持以“-”分隔的日期格式。

let date ='2021-01-01 00:00:00';
console.log(new Date(date).getTime());

如上代码在苹果微信小程序中就无法显示。

 

解决方法,使用正则替换日期中的“-”为“/”。

let date ='2021-01-01 00:00:00';
date = date.replace(/-/g, '/');
console.log(new Date(date).getTime());

 

 

 

暂无评论

发送评论 编辑评论