如何使用 momentjs 在 UTC 中获取昨天的开始时间
how to get yesterday starttime in UTC using momentjs
我想用momentjs获取纪元秒
例如,现在时间是 2019-04-20T15:07:04.388Z
EST,我想得到昨天开始时间 2019-04-19T00:00:00.000Z
的 UTC。
我试过下面的代码 -
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
。但我希望它在 UTC 中是 Fri, 19 Apr 2019 00:00:00 +0000
。
我们代码中使用的momentjs版本-
"moment": "2.24.0",
"moment-timezone": "^0.5.23"
知道我怎样才能得到这个吗?
你可以试试这一行语句
console.log( moment.utc().subtract(1, 'days').startOf('day').toString());
输出:2019 年 4 月 21 日星期日 00:00:00 GMT+0000
我想用momentjs获取纪元秒
例如,现在时间是 2019-04-20T15:07:04.388Z
EST,我想得到昨天开始时间 2019-04-19T00:00:00.000Z
的 UTC。
我试过下面的代码 -
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
。但我希望它在 UTC 中是 Fri, 19 Apr 2019 00:00:00 +0000
。
我们代码中使用的momentjs版本-
"moment": "2.24.0",
"moment-timezone": "^0.5.23"
知道我怎样才能得到这个吗?
你可以试试这一行语句
console.log( moment.utc().subtract(1, 'days').startOf('day').toString());
输出:2019 年 4 月 21 日星期日 00:00:00 GMT+0000