Moment.js - UTC gives an invalid date

Why time.js UTC always shows the wrong date. For example, from the Chrome 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 it returns 17th instead of the 18th that was transmitted.

But if I use momentjs without utc:

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

I return "2013-07-18" , as I expect when using a point in time. js UTC.

Does this mean that we cannot get the correct date when using a point in time. js UTC?

+49
javascript timezone datetime momentjs
Jul 25 '13 at 10:44
source share
2 answers

By default, MomentJS parses in local time. If only a date string is provided (no time), the default value is used until midnight.

In your code, you create a local date and then convert it to the UTC time zone (in fact, it switches the instance of the instance to UTC ), so when it is formatted, it moves (depending on your local time) forward or backward.

If the local time zone is UTC + N (N is a positive number) and you parse the string for date only, you will get the previous date.

Here are some examples to illustrate this (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 and time string to be interpreted as UTC, you must be explicit:

 >>> 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 ) parse it as a UTC date using moment.utc() and include the format string as the 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 switch sides and convert the 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" 
+79
Jul 25 '13 at 11:29
source share

Both Date and moment will parse the input string in the default local time zone of the browser. However, Date sometimes inconsistent with this. 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 correspond to how it analyzes.

The correct way to analyze the input time in UTC format in the format you specify is as follows:

 moment.utc('07-18-2013', 'MM-DD-YYYY') 

Refer to this documentation .

If you want to format it differently for output, you will do the following:

 moment.utc('07-18-2013', 'MM-DD-YYYY').format('YYYY-MM-DD') 

You do not need to explicitly call toString .

Please note that it is very important to provide an input format. Without it, a date similar to 01-04-2013 can be processed both on January 4 and April 1, depending on the browser culture settings.

+20
Jul 25 '13 at 13:24
source share



All Articles