moment.js - UTC不能像我期望的那样工作

her*_*w78 8 javascript utc node.js momentjs

在节点控制台中测试:

var moment = require('moment');

// create a new Date-Object
var now = new Date(2013, 02, 28, 11, 11, 11);

// create the native timestamp
var native = Date.UTC(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours(), now.getMinutes(), now.getSeconds());

// create the timestamp with moment
var withMoment = moment.utc(now).valueOf()
// it doesnt matter if i use moment(now).utc().valueOf() or moment().utc(now).valueOf()

// native: 1364469071000
// withMoment: 1364465471000
native === withMoment // false!?!?! 

// this returns true!!!
withMoment === now.getTime()
Run Code Online (Sandbox Code Playgroud)

为什么本机的时间戳与wit​​hMoment相同?为什么withMoment返回从当前本地时间计算的时间戳?我怎么能实现那个moment.utc()返回与Date.UTC()相同的?

rob*_*lep 11

moment.utc()以与您呼叫相同的方式拨打电话Date.UTC:

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

我认为呼叫moment.utc(now)将使它now在当地时区假设生命,它将首先将其转换为UTC,因此存在差异.