ios时间戳转换为NaN问题

530 阅读1分钟

在安卓机中时间戳转换正常,可以正常显示, 但在ios中转换出来的是NaN

原因是低版本ios不识别以 - 分割的时间格式

百度中很多人推荐使用 var newDate=new Date("2022-07-30T16:00:00.000+0000".replace(/-/g,"/"))

转换出来的是 2022/07/30T16:00:00.000+0000

发现还是不行 问题出在 T 上面, 把这 T 替换成空格就行了

附上兼容ios 安卓转换时间戳代码

filterDay(value){
    if(value){
            let newTime = value.replace(/\-/g,"/")
            console.log('newTime',value)
            console.log('newTime',newTime)
            let date = new Date(newTime.slice(0, 10) + " " + newTime.slice(11))
            let Y = date.getFullYear();
            // 在日期格式中,月份是从0开始的,因此要加0,使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
            let M = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            let D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            let h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            let m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
            // 拼接
            // return Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s;
            // Y年 M月 D日 h时 m分 s秒 可根据自身情况来自定义拼接
            return Y + "-" + M + "-" + D;
				}