node.js MomentJS 在 UTC 中获取 JavaScript 日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26873200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:06:43  来源:igfitidea点击:

MomentJS getting JavaScript Date in UTC

node.jsmomentjs

提问by Cmag

I am not able to get the JavaScript Date string for MongoDB record via the following. It keeps using my local time.

我无法通过以下方式获取 MongoDB 记录的 JavaScript 日期字符串。它一直使用我的当地时间。

var utc = moment.utc().valueOf();
console.log(moment.utc(utc).toDate());

Output:

输出:

Tue Nov 11 2014 14:42:51 GMT-0500 (EST)

I need it to be in UTC, so I can stick this timestamp in Mongo so type would be Date.

我需要它是 UTC,所以我可以把这个时间戳记在 Mongo 中,所以类型是Date.

How can I do that?

我怎样才能做到这一点?

回答by clay

A timestamp is a point in time. Typically this can be represented by a number of milliseconds past an epoc (the Unix Epoc of Jan 1 1970 12AM UTC). The format of that point in time depends on the time zone. While it is the same point in time, the "hours value" is not the same among time zones and one must take into account the offset from the UTC.

时间戳是一个时间点。通常,这可以用 epoc(UTC 时间 1970 年 1 月 1 日凌晨 12 点的 Unix Epoc)过去的毫秒数表示。该时间点的格式取决于时区。虽然它是同一时间点,但时区之间的“小时值”并不相同,必须考虑到UTC的偏移量。

Here's some code to illustrate. A point is time is captured in three different ways.

这里有一些代码来说明。一个点是以三种不同的方式捕获时间。

var moment = require( 'moment' );

var localDate = new Date();
var localMoment = moment();
var utcMoment = moment.utc();
var utcDate = new Date( utcMoment.format() );

//These are all the same
console.log( 'localData unix = ' + localDate.valueOf() );
console.log( 'localMoment unix = ' + localMoment.valueOf() );
console.log( 'utcMoment unix = ' + utcMoment.valueOf() );

//These formats are different
console.log( 'localDate = ' + localDate );
console.log( 'localMoment string = ' + localMoment.format() );
console.log( 'utcMoment string = ' + utcMoment.format() );
console.log( 'utcDate  = ' + utcDate );

//One to show conversion
console.log( 'localDate as UTC format = ' + moment.utc( localDate ).format() );
console.log( 'localDate as UTC unix = ' + moment.utc( localDate ).valueOf() );

Which outputs this:

输出这个:

localData unix = 1415806206570
localMoment unix = 1415806206570
utcMoment unix = 1415806206570
localDate = Wed Nov 12 2014 10:30:06 GMT-0500 (EST)
localMoment string = 2014-11-12T10:30:06-05:00
utcMoment string = 2014-11-12T15:30:06+00:00
utcDate  = Wed Nov 12 2014 10:30:06 GMT-0500 (EST)
localDate as UTC format = 2014-11-12T15:30:06+00:00
localDate as UTC unix = 1415806206570

In terms of milliseconds, each are the same. It is the exact same point in time (though in some runs, the later millisecond is one higher).

就毫秒而言,每个都是相同的。它是完全相同的时间点(尽管在某些运行中,较晚的毫秒会高 1 毫秒)。

As far as format, each can be represented in a particular timezone. And the formatting of that timezone'd string looks different, for the exact same point in time!

至于格式,每个都可以在特定的时区中表示。对于完全相同的时间点,该时区字符串的格式看起来不同!

Are you going to compare these time values? Just convert to milliseconds. One value of milliseconds is always less than, equal to or greater than another millisecond value.

您要比较这些时间值吗?只需转换为毫秒。一个毫秒值总是小于、等于或大于另一个毫秒值。

Do you want to compare specific 'hour' or 'day' values and worried they "came from" different timezones? Convert to UTC first using moment.utc( existingDate ), and then do operations. Examples of those conversions, when coming out of the DB, are the last console.logcalls in the example.

您想比较特定的“小时”或“天”值并担心它们“来自”不同的时区吗?首先使用 转换为UTC moment.utc( existingDate ),然后进行操作。这些转换的示例,当从数据库中出来时,是console.log示例中的最后一次调用。

回答by Timothy Gonzalez

Calling toDate will create a copy(the documentation is down-right wrong about it not being a copy), of the underlying JS Date object. JS Date object is stored in UTC and will always print to eastern time. Without getting into whether .utc() modifies the underlying object that moment wraps use the code below.

调用 toDate 将创建一个底层 JS Date 对象的副本(文档关于它不是副本是完全错误的)。JS 日期对象以 UTC 格式存储,并将始终打印为东部时间。不考虑 .utc() 是否修改了 moment 包装的底层对象,请使用下面的代码。

You don't need moment for this.

你不需要这个时间。

new Date().getTime()

This works, because JS Date at its core is in UTC from the Unix Epoch. It's extraordinarily confusing and I believe a big flaw in the interface to mix local and UTC times like this with no descriptions in the methods.

这是有效的,因为 JS Date 的核心是来自 Unix Epoch 的 UTC。这是非常令人困惑的,我相信接口中的一个很大缺陷,将本地时间和 UTC 时间像这样混合在一起,而方法中没有描述。

回答by av-k

Or simply:

或者干脆:

Date.now

Date.now

From MDN documentation:

来自MDN 文档

The Date.now() method returns the number of milliseconds elapsed since January 1, 1970

Date.now() 方法返回自 1970 年 1 月 1 日以来经过的毫秒数

Available since ECMAScript 5.1

ECMAScript 5.1起可用

It's the same as was mentioned above (new Date().getTime()), but more shortcutted version.

它与上面提到的 ( new Date().getTime()) 相同,但更快捷的版本。