8

ノード コンソールでのテスト:

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()

ネイティブが withMoment と同じタイムスタンプではないのはなぜですか? withMoment が現在の現地時間から計算されたタイムスタンプを返すのはなぜですか? moment.utc() が Date.UTC() と同じものを返すようにするにはどうすればよいですか?

4

2 に答える 2

11

moment.utc()あなたが呼んでいるのと同じ方法で呼んでくださいDate.UTC

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

moment.utc(now)電話をかけると、ローカルタイムゾーンに住んでいると想定nowされ、最初にUTCに変換されるため、違いが生じると思います。

于 2013-02-28T11:26:07.063 に答える
3

あなたがしていることは本質的にこれです。

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

nowは現在のタイムゾーンで構築され、UTC で構築されるためnative、オフセットによって異なります。午前 11 時 PST != 午前 11 時 GMT。

于 2013-02-28T17:14:52.913 に答える