# Moment JavaScript 中获取 Unix 时间戳

**URL:** <https://www.isharkfly.com/t/moment-javascript-unix/16185>\
**Category:** 前端\
**Tags:** javascript\
**Created:** [August 23, 2024, 1:47am UTC](https://www.isharkfly.com/t/moment-javascript-unix/16185 "2024-08-23T01:47:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hex](https://www.isharkfly.com/user_avatar/www.isharkfly.com/hex/32/21953_2.png) [@hex](https://www.isharkfly.com/u/hex)\
**Post date:** [August 23, 2024, 1:47am UTC](https://www.isharkfly.com/t/moment-javascript-unix/16185/1 "2024-08-23T01:47:25Z")

</div>

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

```auto
import moment from 'moment';

const nowUTC = moment.utc();

console.log(nowUTC.unix());

```

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

 ![2024-11-01_17-02-48](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/c/c6846029930b3713578f6d67dcba0a8a065526f0.png)

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

## 毫秒

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

```auto
console.log(nowUTC.valueOf());

```

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

 ![2024-11-01_17-07-14](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/c/cfdac9c1549ab9d66593156f6bf5d0ae9a0e687f.png)

具体的输出情况如下图。

 ![2024-11-01_17-08-06](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/0/0784031cec78c4787555b7887c4f02fcadc09f68.png)

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

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [November 1, 2024, 9:00pm UTC](https://www.isharkfly.com/t/moment-javascript-unix/16185/2 "2024-11-01T21:00:46Z")

</div>


