javascript moment.js - UTC 给出错误的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17855842/
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
moment.js - UTC gives wrong date
提问by brg
Why does moment.js UTC always show the wrong date. For example from chrome's developer console:
为什么moment.js UTC 总是显示错误的日期。例如来自 chrome 的开发者控制台:
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 17thinstead of 18th, that was passed in.
他们都将返回"2013-07-17"为什么它返回17th而不是18th,这是传入的。
But if I use momentjs without the utc:
但是如果我使用没有 utc 的 momentjs:
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.
我返回“2013-07-18”,这也是我在使用 moment.js UTC 时所期望的。
Does this mean we cannot get the correct date when using moment.js UTC?
这是否意味着我们在使用 moment.js UTC 时无法获得正确的日期?
回答by MasterAM
By default, MomentJS parses in local time. If only a date string (with no time) is provided, the time defaults to midnight.
默认情况下,MomentJS 在本地时间解析。如果仅提供日期字符串(没有时间),则时间默认为午夜。
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.
在您的代码中,您创建一个本地日期,然后将其转换为 UTC 时区(实际上,它使 moment 实例切换到UTC 模式),因此在对其进行格式化时,它会向前移动(取决于您的本地时间)或向后。
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.
如果本地时区是 UTC+N(N 是一个正数),并且您解析一个仅限日期的字符串,您将获得上一个日期。
Here are some examples to illustrate it (my local time offset is UTC+3 during DST):
以下是一些示例来说明它(我的本地时间偏移量在 DST 期间为 UTC+3):
>>> 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:
如果您希望将日期时间字符串解释为 UTC,您应该明确说明它:
>>> 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()
格式字符串作为第二个参数将其解析为 UTC 日期,以防止歧义。
>>> 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:
要反过来将 UTC 日期转换为本地日期,您可以使用该local()
方法,如下所示:
>>> moment.utc('07-18-2013', 'MM-DD-YYYY').local().format("YYYY-MM-DD HH:mm")
"2013-07-18 03:00"
回答by Matt Johnson-Pint
Both Date
and moment
will parse the input string in the local time zone of the browser by default. However Date
is sometimes inconsistent with this regard. If the string is specifically YYYY-MM-DD
, using hyphens, or if it is YYYY-MM-DD HH:mm:ss
, it will interpret it as local time. Unlike Date
, moment
will always be consistent about how it parses.
双方Date
并moment
会在解析默认浏览器的本地时区输入字符串。然而Date
有时与这方面不一致。如果字符串是特定的YYYY-MM-DD
,使用连字符,或者如果是YYYY-MM-DD HH:mm:ss
,它会将其解释为本地时间。与Date
,moment
将始终保持一致的解析方式。
The correct way to parse an input moment as UTC in the format you provided would be like this:
以您提供的格式将输入时刻解析为 UTC 的正确方法如下:
moment.utc('07-18-2013', 'MM-DD-YYYY')
Refer to this documentation.
请参阅此文档。
If you want to then format it differently for output, you would do this:
如果您想为输出设置不同的格式,您可以这样做:
moment.utc('07-18-2013', 'MM-DD-YYYY').format('YYYY-MM-DD')
You do not need to call toString
explicitly.
您不需要toString
显式调用。
Note that it is very important to provide the input format. Without it, a date like 01-04-2013
might get processed as either Jan 4th or Apr 1st, depending on the culture settings of the browser.
请注意,提供输入格式非常重要。没有它,像这样的日期01-04-2013
可能会被处理为 1 月 4 日或 4 月 1 日,具体取决于浏览器的文化设置。