Luckylooke Luckylooke - 7 days ago 8
Javascript Question

Moment.js giving tomorow and yesterday in reverse

My console:

enter image description here

same w/o new Date constructor:

enter image description here

What can be wrong??

PS: it is not project related because I am getting same results in console on momentjs.com

EDIT:
To easily reproduce, go to momentjs.com, open console, put:

moment().calendar('2016-11-27T07:31:29.000+0000'); // expecting "Sunday at 9:58 AM" getting "Tuesday at 9:58 AM"
moment().calendar('2016-11-28T07:31:29.000+0000'); // expecting "Yesterday at 9:58 AM" getting "Tomorrow at 9:58 AM"
moment().calendar('2016-11-29T07:31:29.000+0000'); // this is correct "Today at 9:58 AM"
moment().calendar('2016-11-30T07:31:29.000+0000'); // expecting "Tomorrow at 9:58 AM" getting "Yesterday at 9:58 AM"

Answer

In the docs it says:

moment().calendar(referenceTime);

Calendar time displays time relative to a given referenceTime (defaults to now), but does so slightly differently than moment#fromNow

So the results what you get is correct, because your string time is reference.

For the correct result use:

moment('2016-11-30T07:31:29.000+0000').calendar();
Comments