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?
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?
You can use the isSameOrAfter
method in momentjs:
moment1.isSameOrAfter(moment2);
isSameOrBefore
is also available –
Jokester 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
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
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');
© 2022 - 2024 — McMap. All rights reserved.