js怎样获取到时间戳?

1.获取到时间戳精确到秒,13位;

javascript 复制代码
let timestamp = Date.parse(new Date());
console.log(timestamp);

//输出 1591669256000   13位

2.获取到时间戳精确到毫秒,13位;

javascript 复制代码
let timestamp = Math.round(new Date());
console.log(timestamp);

//输出 1591669961203   13位

3.获取到时间戳精确到毫秒,13位;

javascript 复制代码
const timestamp = (new Date()).valueOf();
console.log(timestamp);

//输出 1591670037603   13位

4.获取到时间戳精确到毫秒,13位;

javascript 复制代码
let timestamp = new Date().getTime();
console.log(timestamp);

//输出 1591670068833   13位

5.获取到时间戳精确到毫秒,13位;

javascript 复制代码
let timestamp = +new Date();
console.log(timestamp);

//输出 1591670099066   13位

以上是获取到时间戳的方法,可以根据需求挑选。

相关推荐
Julien200421 分钟前
管理机密(一)
运维·服务器·开发语言·学习方法
Chen_harmony24 分钟前
七、STL与string类
开发语言·c++
沐晓时光30 分钟前
C语言入门,字符串和字符串函数知识讲解
c语言·开发语言
xxxiugou1231 小时前
C++命名空间与引用全解析
开发语言·c++
独隅1 小时前
WebStorm Windows 版 React / Next.js 实战替换指南
javascript·windows·webstorm
冷眸_1 小时前
同名食材不能直接相减:一个 JavaScript 用量模型的边界
javascript
ruanno_top2 小时前
倒计时该用 deadline 还是每秒减一?我把两种写法都跑了一遍
vue.js
apollo_qwe2 小时前
接口超时了,前端为什么不能直接重试?
javascript·http
yangmu32032 小时前
编程语言发展简史:从机器指令到智能协作
开发语言
Pniubi5 小时前
Java为什么要分堆和栈
java·开发语言