JS-日期转换为时间戳、new date(time).getTime()兼容IOS

详情参考:https://timor419.github.io/2020/03/28/JS-dateToTimestamp/

const date = new Date('2032-01-27 5:38:13:123');
const time1 = date.getTime(); //会精确到毫秒
const time2 = date.valueOf(); //会精确到毫秒
const time3 = Date.parse(date); //只能精确到秒,毫秒用000替代
console.log(time1); //1958765893123
console.log(time2); //1958765893123
console.log(time3); //1958765893000

封装成公共方法

说明:在处理时间戳的时候,new Date(data).get Time()在IOS系统上有兼容问题,需要把时间字符串中的“-”替换成“/”。

不仅是iOS,IE浏览器也有这个问题,办法同样适用

// 把时间日期转成时间戳
getTimestamp(time) {
	const getDay = new Date(time.replace(/-/g, '/')).getTime(); // new Date(time).getTime()兼容IOS
	return getDay;
},

调用公共方法

console.log(this.getTimestamp('2020-03-28 00:00:00')); // 1585324800000

------------- The End -------------

许可协议: 转载请保留原文链接及作者。

猜你喜欢

转载自blog.csdn.net/weixin_43937466/article/details/105166079