JavaScript 获取时间戳的5种方法

1.Date.parse(new Date())

const timestamp = Date.parse(new Date());
console.log(timestamp);
//输出 1591669256000   13位

2.Math.round(new Date())

const timestamp = Math.round(new Date());
console.log(timestamp);  
//输出 1591669961203   13位

3.(new Date()).valueOf()

const timestamp = (new Date()).valueOf();
console.log(timestamp);
//输出 1591670037603   13位

4.new Date().getTime()

const timestamp = new Date().getTime();
console.log(timestamp);
//输出 1591670068833   13位

5.+new Date()

const timestamp = +new Date();
console.log(timestamp);
//输出 1591670099066   13位

在开发的中需要精确到秒的时候,推荐使用 第1种方法,也需要除以1000才行,如果是需要时间戳毫秒的推荐 +new Date() 和 new Date().getTime();

猜你喜欢

转载自blog.csdn.net/qq_68862343/article/details/131522476