Reputation: 5357
I've encountered a strange result when comparing two MomentJS objects using max and min methods. They appear to return the wrong value. For example, this code returns today rather than tomorrow:
moment().max(moment().add(1, 'd'))
Can anyone explain this behavior?
Upvotes: 4
Views: 4942
Reputation: 22905
You misinterpret the meaning of min
and max
.
From the test suite (https://github.com/moment/moment/blob/develop/test/moment/min_max.js#L51):
equalMoment(test, now.max(future), now, "Now with the maximum of the future should be now");
The way to understand the meaning is: a.max(b) <= b
(at latest, the result can be the second date).
The documentation has a clear quote:
Sometimes, server clocks are not quite in sync with client clocks. This ends up displaying humanized strings such as "in a few seconds" rather than "a few seconds ago". You can prevent that with moment#max()
The .max
function is therefore the numerical minimum (selecting the earlier moment)
Upvotes: 2
Reputation: 938
After looking in the source code of MomentJS 2.2.1, here's the source code of max():
max: function ( other ) {
other = moment.apply( null, arguments );
return other > this ? this : other;
},
Seems like they are returning this
when other
is later.. Weird..
Upvotes: 0