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

127,588

Solution 1

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

myMoment.diff(yourMoment) >= 0

Close enough.

Solution 2

You can use the isSameOrAfter method in momentjs:

moment1.isSameOrAfter(moment2);

Solution 3

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

Share:
127,588
johntrepreneur
Author by

johntrepreneur

I’m an entrepreneur at heart and currently doing web development using Microsoft technologies for the foundation (ASP.NET and SQL Server), but with a strong emphasis on client side implementation (Javascript/JQuery) given the nature of my most recent work. My previous experience and majority of my professional career was doing web services SOAP/REST at Intuit with J2EE, CI builds, code coverage, code reviews, scrum, and the works. I graduated from University of Georgia in 1997 with a BBA-MIS degree. GO DAWGS!!!

Updated on September 15, 2021

Comments

  • johntrepreneur
    johntrepreneur over 2 years

    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?