Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 420
johntrepreneur Avatar asked Dec 06 '14 00:12

johntrepreneur


People also ask

What is Moment () hour ()?

The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().

Why you shouldnt use MomentJS?

However, Moment. js has too many drawbacks compared to modern date and time libraries. Its API is not immutable, it is large and it doesn't support tree shaking. Even the Moment team discourages to use their library in new projects.


3 Answers

You can use the isSameOrAfter method in momentjs:

moment1.isSameOrAfter(moment2);
like image 151
Daniel0b1b Avatar answered Oct 23 '22 03:10

Daniel0b1b


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

myMoment.diff(yourMoment) >= 0

Close enough.

like image 25
johntrepreneur Avatar answered Oct 23 '22 02:10

johntrepreneur


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

like image 12
tuananh Avatar answered Oct 23 '22 01:10

tuananh