98

Basically, I want to do a myMoment >= yourMoment. There is no myMoment.isSameOrAfter and writing that out combining isSame and .isAfter is a bit lengthy.

What's the alternative? Convert moment to js date and use >= to compare?

johntrepreneur
  • 4,282
  • 4
  • 36
  • 50
  • 5
    Have you tried a if(!moment1.isBefore(moment2)) ? – SamHuckaby Dec 06 '14 at 00:42
  • 1
    @Donal - well I'm debugging in chrome and noticing myMoment == yourMoment is false, but myMoment.isSame(yourMoment) is true. – johntrepreneur Dec 06 '14 at 00:57
  • @donal JavaScript does not support operator overloading, and so relational operators like and >= are actually doing reference comparison. So, if it "works", it is a complete accident and due to the fact its comparing primitive values for these complex objects. – John Zabroski Aug 06 '15 at 01:49
  • 1
    @JohnZabroski that's not true, JS is using the valueOf prototype method to compare, so operators like >, >=, < and <= are valid on Moment instances since they will use the valueOf i.e. the timestamps. – antoine129 Apr 11 '18 at 13:36
  • @antoine129 I repeat, it is due to the fact its comparing primitive values for these complex objects. valueOf is how JavaScript gets the primitive value. It is not a guarantee that this value is even a number. – John Zabroski Apr 11 '18 at 15:11
  • 2
    @JohnZabroski I repeat, it is guaranteed to be a Number in the case of Moment.js: https://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/ and see also https://stackoverflow.com/questions/22600856/moment-js-date-time-comparison#comment79617624_22601019 – antoine129 Apr 12 '18 at 15:50

3 Answers3

194

You can use the isSameOrAfter method in momentjs:

moment1.isSameOrAfter(moment2);
thanksd
  • 50,371
  • 21
  • 151
  • 145
Daniel0b1b
  • 2,082
  • 2
  • 10
  • 9
64

Okay, I just went with the moment.js .diff() function.

myMoment.diff(yourMoment) >= 0

Close enough.

The Red Pea
  • 14,933
  • 15
  • 89
  • 118
johntrepreneur
  • 4,282
  • 4
  • 36
  • 50
  • 1
    What do you mean, "close enough"? It's exactly the right behavior. If you want isOnOrAfter, just add a function to the Moment prototype. – John Zabroski Aug 06 '15 at 01:51
  • 1
    It's worth mentioning diff returns the difference in milliseconds, so if you're thinking about expanding this by saying next week is diff(yourMoment) >= 7 you will fail. Also, don't just add milliseconds in a day,because time zones, etc. will mess you up. – Nick Steele Apr 04 '16 at 12:10
  • 4
    follow up to @NickSteele's comment, you can use it for different time measures with the second param. e.g: (myMoment.diff(yourMoment, 'minutes') >= 15) or (myMoment.diff(yourMoment, 'days') >= 7) – kiwicopple Jul 16 '18 at 04:34
10
let A = moment('2020-01-02');
let B = moment('2020-01-01');
let C = moment('2020-01-03');
console.log(A.diff(B, 'days'));// => 1
console.log(A.diff(C, 'days'));// => -1

The supported measurements are years, months, weeks, days, hours, minutes, and seconds momentjs.com

tuananh
  • 657
  • 8
  • 16