100

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,514
  • 6
  • 39
  • 52
  • 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. – Antoine 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
  • 3
    @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 – Antoine Apr 12 '18 at 15:50

5 Answers5

213

You can use the isSameOrAfter method in momentjs:

moment1.isSameOrAfter(moment2);
thanksd
  • 54,176
  • 22
  • 157
  • 150
Daniel0b1b
  • 2,271
  • 2
  • 11
  • 9
67

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

myMoment.diff(yourMoment) >= 0

Close enough.

Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
johntrepreneur
  • 4,514
  • 6
  • 39
  • 52
  • 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
14
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
  • 747
  • 8
  • 17
3

Moment does implement the valueOf() method. Therefor < <= > >= all can coerce moment into a native type. If you look at the actual implementations for isAfter or isBefore you'll see that's exactly what they do.

So you can just do myMoment >= yourMoment

Jim Sosa
  • 578
  • 3
  • 21
2

You could use isBetween function that compares one date in a range:

let foundDateInRange = moment('2022-01-15').isBetween('2022-01-01', '2022-01-30');

Console image using isBetween method

Oleg Barabanov
  • 2,468
  • 2
  • 8
  • 17