javascript - 将 Moment utc 时间转换为 unix 时间

标签 javascript momentjs

大家好,我想知道如何使用库 moment.js 将日期转换为 Unix 时间戳,以便将 oldDate 与另一个日期进行比较。

这是我尝试过的:

var oldDate = (moment.unix(1490632174)).format();
// here I got the Date in string format
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
// now I want to convert it again into unix timestamp and I don't know how to do it.
console.log(oldDate, newDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

最佳答案

文档是一件很棒的事情。 Unix Timestamp (seconds)

moment().unix();

moment#unix outputs a Unix timestamp (the number of seconds since the Unix Epoch).

moment(1318874398806).unix(); // 1318874398

This value is floored to the nearest second, and does not include a milliseconds component.

var oldDate = moment.unix(1490632174).unix();
// here I got the Date in string format
var newDate= moment.utc('2017-03-27T18:29:59+02:00', "YYYY-MM-DD");
// now I want to convert it again into unix timestamp and I don't know how to do it.
console.log(oldDate, newDate.unix());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

关于javascript - 将 Moment utc 时间转换为 unix 时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43639544/

相关文章:

javascript - moment 无法解析我的日期是什么? (年月日日)

angularjs - Moment.js 未捕获的语法错误

javascript - 如何将值从父应用程序传递到在 Angular JS 的 iFrame 中加载的应用程序?

javascript - Paper.js 外部文件不会加载

javascript - 使用 javascript 或 momentjs 的时间范围

javascript - 使用 on 获取生成的日期并重用它 jquery

javascript - Angular Date 管道,以及一个带有 Momentjs 的简单计时器

javascript - 检查所有输入字段是否都有值 jQuery 条件

javascript - 发生 uncaughtException 时的 Node JS 设置消息

javascript - 创建一个更改日志表,记录下拉菜单中的更改