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

查看:53
本文介绍了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" 为什么它返回 17th 而不是 18th,这是传入的.

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

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

我回来了 "2013-07-18" 这也是我在使用 moment.js UTC 时所期望的.

这是否意味着我们在使用 moment.js UTC 时无法获得正确的日期?

解决方案

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

在您的代码中,您创建了一个本地日期,然后将其转换为 UTC 时区(实际上,它使时刻实例切换到 UTC 模式),因此在格式化时,它会向前或向后移动(取决于您的当地时间).

如果本地时区是 UTC+N(N 是一个正数),并且您解析一个仅限日期的字符串,您将获得上一个日期.

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

<预><代码>>>>时刻('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">>>日期()2013 年 7 月 25 日星期四 14:28:45 GMT+0300(耶路撒冷夏令时)"

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

<预><代码>>>>时刻(新日期('07-18-2013 UTC')).utc().format("YYYY-MM-DD HH:mm")2013-07-18 00:00"

或者,正如 Matt Johnson 在他的回答中提到的,您可以(并且可能应该)首先使用 moment.utc() 并包含格式字符串作为第二个参数以防止歧义.

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

要反过来将 UTC 日期转换为本地日期,您可以使用 local()方法,如下:

<预><代码>>>>moment.utc('07-18-2013', 'MM-DD-YYYY').local().format("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天全站免登陆