javascript - Dates display behind by a day -


Moment. I am facing some strange behavior while using JS. I have some helpful classes attached to the date assistant prototype, which are apparently being created to display every day a day later.

  Date.prototype.format = function () {return moment (this) .format ('MM / DD / YYYY')} var date = new date ('2008-05-13T00: 00: 00 ') date.format () // = & gt; 05/12/2008, but should be 05/13/2008  

I have noticed that some other weird things:

  date.getDate () / / = & Gt; The yield should be 12, but should be 13  

But if I change the instant object directly with the Utiisi string, then it works:

 < Code> moment ('2008 -05-13T 00: 00: 00'). Format ('MM / DD / YY') // = & gt; 05/13/08  

But I'm working with plain date objects, and amending every date in a moment object is not my favorite idea to remove the UTC string from the date I have already tried to modify the format function and see that it is displayed correctly, but there is no benefit.

  date.toUTCString () // = & gt; Correctly yields "Mars, 13 May 2008 00:00:00 GMT" moment (date.toUTCString ()). Format ('MM / DD / YY') // = & gt; Still 05/12/08  

Any ideas what's going on here? Is there a problem with the date constructor?

Edit: Output as time:

  moment (date). Format ('MM / DD / Y YHH: mm: SS') // = & gt; "05/12/08 08:00:00"  

You have to tell the moment. Js that you want to display on date in UTC:

  moment (this) .UTC () Format ('MM / DD / YYYY')  

More in the document:


But, if I immediately with the UTC string Immediately transform the moment object, so it works:

  moment ('2008-05-13T00: 00: 00'). Format ('MM / DD / YY') // = & gt; Moment.js:  

By default, instant pars and displays in local time.

While new date () (and dated .pars ):

The parse function [...] interprets the string as a date and time; It gives the UTC time value related to a number, date and time.


I have already tried to modify the format function to remove the date UTC string and see that it appears correctly, but no one Not benefited

  date.toUTCString () // = & gt; Correctly yields "Mars, 13 May 2008 00:00:00 GMT" moment (date.toUTCString ()). Format ('MM / DD / YY') // = & gt; Still, 05/12/08  

format which is not in any of the date.toUTCString () yield, so it falls back Using new date () (which interprets the string as UTC time, not local time).


Comments