MomentJS在UTC中获取JavaScript日期 [英] MomentJS getting JavaScript Date in UTC

查看:761
本文介绍了MomentJS在UTC中获取JavaScript日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

输出:

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

我需要将它设置为UTC,因此我可以将此时间戳记在Mongo中,因此类型应为Date.

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

我该怎么做?

推荐答案

时间戳是一个时间点.通常,这可以通过经过epoc(1970年1月1日,美国东部时间UTC的Unix Epoc)后的毫秒数来表示.该时间点的格式取决于时区.虽然是同一时间点,但各个时区之间的小时值"并不相同,因此必须考虑与 UTC .

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.

这里有一些代码需要说明.重要的是时间是通过三种不同的方式捕获的.

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

哪个输出:

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

就格式而言,每个都可以在特定时区中表示.而且,该时区字符串的格式看起来完全不同,而且时间完全相同!

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.

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

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.log calls in the example.

这篇关于MomentJS在UTC中获取JavaScript日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆