简体   繁体   English

moment.js - UTC提供错误的日期

[英]moment.js - UTC gives wrong date

Why does moment.js UTC always show the wrong date. 为什么moment.js UTC总是显示错误的日期。 For example from chrome's developer console: 例如来自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 17th instead of 18th , that was passed in. 他们两人都会回归“2013-07-17”,为什么它会回归17号而不是18号 ,这是传递给他们的。

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 default, MomentJS parses in local time. 默认情况下,MomentJS在本地时间解析。 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. 在您的代码中,您创建一个本地日期,然后将其转换为UTC时区(实际上,它会使当前实例切换到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. 或者,正如Matt Johnson在他的回答中提到的那样,您可以( 并且可能应该 )使用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"

Both Date and moment will parse the input string in the local time zone of the browser by default. 默认情况下, Datemoment都将在浏览器的本地时区中解析输入字符串。 However Date is sometimes inconsistent with this regard. 但是, Date有时与这方面不一致。 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 . 如果字符串特别是YYYY-MM-DD ,使用连字符 ,或者如果是YYYY-MM-DD HH:mm:ss ,则将其解释为本地时间 Unlike Date , moment will always be consistent about how it parses. 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日,具体取决于浏览器的文化设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM