0

我想使用 momentjs 获得纪元秒数。

例如,现在的时间是2019-04-20T15:07:04.388ZEST,我想以2019-04-19T00:00:00.000ZUTC 获取昨天的开始时间纪元。

我试过下面的代码 -

    const now = new Date();
    const start = moment(now) // get current datetime
        .utcOffset(0) // convert to UTC
        .subtract(24, "hours") // go 24 hours into the past
        .startOf("day") // get the start of the date we landed on
        .unix(); // get unix timestamp
    console.log(now);
    console.log(start);

上述程序的输出是 -

    Sat Apr 20 2019 15:11:23 GMT-0400 (EDT) {}
    1555650000

根据https://www.unixtimestamp.com/index.php 1555650000 转换为Fri, 19 Apr 2019 05:00:00 +0000. 但我希望它是Fri, 19 Apr 2019 00:00:00 +0000UTC。

我们代码中使用的 momentjs 版本 -

"moment": "2.24.0",
"moment-timezone": "^0.5.23"

知道我怎么能得到这个吗?

4

1 回答 1

0

你可以试试这个单行语句

console.log( moment.utc().subtract(1, 'days').startOf('day').toString()); 

输出:2019 年 4 月 21 日星期日 00:00:00 GMT+0000

于 2019-04-22T01:12:43.727 回答