moment.js - UTC does not work as i expect it

≯℡__Kan透↙ 提交于 2019-12-01 03:55:35

Call moment.utc() the same way you're calling Date.UTC:

var withMoment = moment.utc([now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds()]).valueOf();

I think calling moment.utc(now) will make it assume now lives in the local timezone, and it will convert it to UTC first, hence the difference.

timrwood

What you are doing is essentially this.

var now    = new Date(2013, 02, 28, 11, 11, 11);
var native = Date.UTC(2013, 02, 28, 11, 11, 11);

console.log(now === utc); // false
console.log(now - utc); // your offset from GMT in milliseconds

Because now is constructed in the current timezone and native is constructed in UTC, they will differ by your offset. 11 AM PST != 11 AM GMT.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!