How to do a greater than or equal to with moments (moment.js) in javascript?
Asked Answered
T

5

102

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?

Tigress answered 6/12, 2014 at 0:39 Comment(6)
Have you tried a if(!moment1.isBefore(moment2)) ?Inefficacy
@Donal - well I'm debugging in chrome and noticing myMoment == yourMoment is false, but myMoment.isSame(yourMoment) is true.Tigress
@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.Dillman
@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.Okoka
@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.Dillman
@JohnZabroski I repeat, it is guaranteed to be a Number in the case of Moment.js: momentjs.com/docs/#/displaying/unix-timestamp-milliseconds and see also #22601356Okoka
T
70

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

myMoment.diff(yourMoment) >= 0

Close enough.

Tigress answered 6/12, 2014 at 0:58 Comment(3)
What do you mean, "close enough"? It's exactly the right behavior. If you want isOnOrAfter, just add a function to the Moment prototype.Dillman
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.Figueroa
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)Goldengoldenberg
K
217

You can use the isSameOrAfter method in momentjs:

moment1.isSameOrAfter(moment2);
Kaveri answered 2/8, 2016 at 6:11 Comment(2)
Only available in moment.js version 2.10.7+Tigress
FYI: isSameOrBefore is also availableJokester
T
70

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

myMoment.diff(yourMoment) >= 0

Close enough.

Tigress answered 6/12, 2014 at 0:58 Comment(3)
What do you mean, "close enough"? It's exactly the right behavior. If you want isOnOrAfter, just add a function to the Moment prototype.Dillman
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.Figueroa
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)Goldengoldenberg
C
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

Conquest answered 31/8, 2018 at 8:3 Comment(0)
B
4

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

Borneo answered 10/11, 2022 at 18:3 Comment(0)
M
3

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

Malloch answered 22/8, 2022 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.