将 Moment utc 时间转换为 unix 时间

Def*_*foe 4 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);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

gfo*_*301 6

文档是一件很棒的事情。Unix 时间戳(秒)

moment().unix();

moment#unix 输出 Unix 时间戳(自 Unix 纪元以来的秒数)。

时刻(1318874398806).unix(); // 1318874398

该值取整至最接近的秒,并且不包括毫秒部分。

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());
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Run Code Online (Sandbox Code Playgroud)