moment.js - UTC给出错误的日期 [英] moment.js - UTC gives wrong date

查看:834
本文介绍了moment.js - UTC给出错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么moment.js UTC总是显示错误的日期。例如从chrome的开发者控制台:

  moment(('07-18-2013'))。utc()。format YYYY-MM-DD)。toString()
//或
moment.utc(new Date('07 -18-2013'))。format(YYYY-MM-DD) .toString()

他们都将返回2013-07-17 strong>为什么返回第17个而不是第18个,这是传入的。



但是如果我使用momentjs没有utc:

  moment(new Date('07 -18-2013'))format(YYYY-MM- DD)toString()

我回来了2013-07-18 / strong>这是什么我也期望使用moment.js UTC。



这是否意味着我们无法获取正确的日期,当使用moment.js UTC?

解决方案

默认情况下,MomentJS在本地时间内解析。如果只提供一个日期字符串(没有时间),则默认为午夜。



在代码中,创建本地日期,然后将其转换为UTC时区(实际上,它使时刻实例切换到 UTC模式),所以当格式化时如果本地时区为UTC + N(N为正数),并且您解析日期时间,则会转移(取决于当地时间)向前或向后。





这里有一些例子来说明(我的本地时间偏移量是DST中的UTC + 3):

 >>>时间('07 -18-2013','MM-DD-YYYY')。utc()。format(YYYY-MM-DD HH:mm)
2013-07-17 21:00
>>>时间('07 -18-2013 12:00','MM-DD-YYYY HH:mm')。utc()。format(YYYY-MM-DD HH:mm)
2013-07 -18 09:00
>>>日期()
2013年7月25日14:28:45 GMT + 0300(耶路撒冷日光时间)

如果您希望将日期时间字符串解释为UTC,则应明确说明:

 > >> moment(new Date('07 -18-2013 UTC'))。utc()。format(YYYY-MM-DD HH:mm)
2013-07-18 00:00$ b $或者,正如马特·约翰逊在回答中提到的,你可以(,可能应该)首先使用 moment.utc() ,并将格式字符串作为第二个参数,以防止歧义。

 >>> moment.utc('07 -18-2013','MM-DD-YYYY')格式(YYYY-MM-DD HH:mm)
2013-07-18 00:00

要转到另一个方法,将UTC日期转换为本地日期,您可以使用 local() 方法,如下所示:

 >>> (YYYY-MM-DD HH:mm)
2013-07-18 03: 00


Why does moment.js UTC always show the wrong date. For example from chrome's developer console:

moment(('07-18-2013')).utc().format("YYYY-MM-DD").toString()
// or
moment.utc(new Date('07-18-2013')).format("YYYY-MM-DD").toString()

Both of them will return "2013-07-17" why is it returning 17th instead of 18th, that was passed in.

But if I use momentjs without the utc:

moment(new Date('07-18-2013')).format("YYYY-MM-DD").toString()

I get back "2013-07-18" which is what I also expect when using moment.js UTC.

Does this mean we cannot get the correct date when using moment.js UTC?

解决方案

By default, MomentJS parses in local time. If only a date string (with no time) is provided, the time defaults to midnight.

In your code, you create a local date and then convert it to the UTC timezone (in fact, it makes the moment instance switch to UTC mode), so when it is formatted, it is shifted (depending on your local time) forward or backwards.

If the local timezone is UTC+N (N being a positive number), and you parse a date-only string, you will get the previous date.

Here are some examples to illustrate it (my local time offset is UTC+3 during DST):

>>> moment('07-18-2013', 'MM-DD-YYYY').utc().format("YYYY-MM-DD HH:mm")
"2013-07-17 21:00"
>>> moment('07-18-2013 12:00', 'MM-DD-YYYY HH:mm').utc().format("YYYY-MM-DD HH:mm")
"2013-07-18 09:00"
>>> Date()
"Thu Jul 25 2013 14:28:45 GMT+0300 (Jerusalem Daylight Time)"

If you want the date-time string interpreted as UTC, you should be explicit about it:

>>> moment(new Date('07-18-2013 UTC')).utc().format("YYYY-MM-DD HH:mm")
"2013-07-18 00:00"

or, as Matt Johnson mentions in his answer, you can (and probably should) parse it as a UTC date in the first place using moment.utc() and include the format string as a second argument to prevent ambiguity.

>>> moment.utc('07-18-2013', 'MM-DD-YYYY').format("YYYY-MM-DD HH:mm")
"2013-07-18 00:00"

To go the other way around and convert a UTC date to a local date, you can use the local() method, as follows:

>>> moment.utc('07-18-2013', 'MM-DD-YYYY').local().format("YYYY-MM-DD HH:mm")
"2013-07-18 03:00"

这篇关于moment.js - UTC给出错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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