1.获取时间戳精确到秒,13位
const timestamp = Date.parse(new Date()); console.log(timestamp); //输出 00 13位

2.获取时间戳精确到毫秒,13位
const timestamp = Math.round(new Date()); console.log(timestamp); //输出 03 13位

3.获取时间戳精确到毫秒,13位
const timestamp = (new Date()).valueOf(); console.log(timestamp); //输出 03 13位

4.获取时间戳精确到毫秒,13位
const timestamp = new Date().getTime(); console.log(timestamp); //输出 33 13位

5.获取时间戳精确到毫秒,13位
const timestamp = +new Date(); console.log(timestamp); //输出 66 13位

其它
在开发的中需要精确到秒的时候,推荐使用 第1种方法,也需要除以1000才行,如果是需要时间戳毫秒的推荐 +new Date() 和 new Date().getTime();
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/210514.html原文链接:https://javaforall.net
