我的时间格式又出错了。
今天测试给我复现了一个bug,页面上的时间,在安卓和谷歌浏览器上显示正常,但是在苹果手机上多了8小时。
问题出在哪呢?
截个图展示一下 后端传来的数据:
将这两个时间字符串直接new Date()转换成时间后,会导致苹果手机的时间显示错误。那么合理的处理方式就是
你直接把T替换掉,然后格式化
// 字符串转换成时间 ios中要把毫秒去掉
function toTime(strTime) {
if (!strTime) {
return '';
}
var myDate = new Date(strTime + '+0800');
if (myDate == 'Invalid Date') {
strTime = strTime.replace(/T/g, ' '); //去掉T
strTime = strTime.replace(/-/g, '/');
strTime = strTime.replace(/\.\d+/, ' ');//去掉毫秒
myDate = new Date(strTime + '+0800');
}
return myDate;
}
参考文章:https://blog.youkuaiyun.com/namechenfl/article/details/90241952