Moment JavaScript 中获取 Unix 时间戳

在 JavaScript 中获取时间戳的方法为:

import moment from 'moment';

const nowUTC = moment.utc();

console.log(nowUTC.unix());

我们需要使用 moment 库,这个库更加方便。

时间戳的输出如上面的数字。

毫秒

如果需要输出毫秒的话,应该使用的方法为:

console.log(nowUTC.valueOf());

根据上面文档的说法,这个方法将会返回时间戳的毫秒数值。

具体的输出情况如下图。

有时候,我们可能也不需要输出这么精确。