JS 如何返回时间戳?

8 min read

在 JavaScript 中,可以使用 getTime() 方法来返回一个日期对象的时间戳。时间戳是指从1970年1月1日 00:00:00 UTC 到特定日期的毫秒数。

const date = new Date();
const timestamp = date.getTime();
console.log(timestamp); // 输出当前日期的时间戳

另外,也可以使用 Date.now() 方法来获取当前日期的时间戳,这种方法更简洁:

const timestamp = Date.now();
console.log(timestamp); // 输出当前日期的时间戳