ios浏览器居然不支持yyyy-MM-dd HH:mm:ss格式
今天发现ios浏览器居然不支持解析yyyy-MM-dd HH:mm:ss格式的字符串日期,例如:2021-2-4 11:05:50 ,这种格式不能够正常解析,经查,ios浏览器是支持yyyy/MM/dd HH:mm:ss格式的,因此找到了解决思路,将yyyy-MM-dd HH:mm:ss格式的日期字符串替换成yyyy/MM/dd HH:mm:ss的日期字符串,然后获取对应的时间戳即可。
//日期格式化函数:参数是:yyyy-MM-dd HH:mm:ss格式的日期字符串
formatTime: function (createTime) {
//将yyyy-MM-dd HH:mm:ss格式的日期字符串替换成yyyy/MM/dd HH:mm:ss的日期字符串
createTime = createTime.replace(/-/g, "/");
//日期格式字符串转换为时间戳
let timeStamp = new Date(createTime).getTime();
if (timeStamp) {
//获取当前时间戳
let nowTimeStamp = Date.parse(new Date());
//差值
let diffTime = Math.abs(nowTimeStamp - timeStamp);
if (
diffTime >= 365 * 24 * 3600 * 1000 &&
diffTime < 3650 * 24 * 3600 * 1000
) {
let num = Math.floor(diffTime / (365 * 24 * 60 * 60 * 1000));
return num + "年前";
} else if (
diffTime >= 30 * 24 * 3600 * 1000 &&
diffTime < 365 * 24 * 3600 * 1000
) {
let num = Math.floor(diffTime / (30 * 24 * 60 * 60 * 1000));
return num + "月前";
} else if (
diffTime >= 7 * 24 * 3600 * 1000 &&
diffTime < 30 * 24 * 3600 * 1000
) {
let num = Math.floor(diffTime / (7 * 24 * 60 * 60 * 1000));
return num + "周前";
} else if (
diffTime >= 1 * 24 * 3600 * 1000 &&
diffTime < 7 * 24 * 3600 * 1000
) {
let num = Math.floor(diffTime / (24 * 60 * 60 * 1000));
return num + "天前";
} else if (diffTime < 24 * 3600 * 1000 && diffTime >= 3600 * 1000) {
let num = Math.floor(diffTime / (60 * 60 * 1000));
return num + "小时前";
} else if (diffTime < 3600 * 1000 && diffTime >= 60 * 1000) {
let num = Math.floor(diffTime / (60 * 1000));
return num + "分钟前";
} else if (diffTime < 60 * 1000 && diffTime > 0) {
let num = Math.floor(diffTime / 1000);
return num + "秒前";
}
}
}