Skip to content

MomentJS - Date Queries

Modern Date/Time: Querying Dates with Moment.js

Section titled “Modern Date/Time: Querying Dates with Moment.js”

Moment.js provides several useful methods for comparing dates and checking their properties, simplifying common logical checks like whether a date falls within a certain range or if it’s a leap year.

As usual, consider the maintenance status of Moment.js and explore alternatives like Day.js or date-fns for new projects, which offer similar querying capabilities.

These methods allow you to compare one moment relative to another:

MethodDescription & Syntax
Is BeforeChecks if a moment is before another moment.
moment().isBefore(Moment|String|Number|Date|Array);
moment().isBefore(Moment|String|..., String); // Optional: specify unit granularity (e.g., 'year', 'month', 'day')
moment('2023-10-26').isBefore('2023-11-01'); // true
moment('2023-10-26').isBefore('2023-10-26', 'day'); // false (same day)
Is SameChecks if a moment is the same as another moment.
moment().isSame(Moment|String|Number|Date|Array);
moment().isSame(Moment|String|..., String); // Optional: specify unit granularity
moment('2023-10-26').isSame('2023-10-26'); // true
moment('2023-10-26T12:00').isSame('2023-10-26T13:00', 'day'); // true (same day)
moment('2023-10-26T12:00').isSame('2023-10-26T13:00', 'hour'); // false (different hour)
Is AfterChecks if a moment is after another moment.
moment().isAfter(Moment|String|Number|Date|Array);
moment().isAfter(Moment|String|..., String); // Optional: specify unit granularity
moment('2023-11-01').isAfter('2023-10-26'); // true
Is Same or BeforeChecks if a moment is the same as or before another moment.
moment().isSameOrBefore(Moment|String|Number|Date|Array);
moment().isSameOrBefore(Moment|String|..., String); // Optional: specify unit granularity
moment('2023-10-26').isSameOrBefore('2023-10-26'); // true
moment('2023-10-25').isSameOrBefore('2023-10-26', 'day'); // true
Is Same or AfterChecks if a moment is the same as or after another moment.
moment().isSameOrAfter(Moment|String|Number|Date|Array);
moment().isSameOrAfter(Moment|String|..., String); // Optional: specify unit granularity
moment('2023-10-26').isSameOrAfter('2023-10-26'); // true
moment('2023-10-27').isSameOrAfter('2023-10-26', 'day'); // true
Is BetweenChecks if a moment is between two other moments.
moment().isBetween(momentLikeStart, momentLikeEnd);
moment().isBetween(start, end, String); // Optional: specify unit granularity
moment().isBetween(start, end, String, String); // Optional: inclusivity '()', '[)', '(]', '[]'
moment('2023-10-26').isBetween('2023-10-01', '2023-10-31'); // true
moment('2023-10-31').isBetween('2023-10-01', '2023-10-31', null, '[]'); // true (inclusive)
moment('2023-10-31').isBetween('2023-10-01', '2023-10-31', null, '()'); // false (exclusive)

These methods check specific characteristics of a moment or a variable:

MethodDescription & Syntax
Is Daylight Saving TimeChecks if the current moment is in daylight saving time.
moment().isDST();
Note: Behavior depends on the underlying JavaScript environment’s ability to determine DST.
Is Leap YearChecks if the moment’s year is a leap year.
moment('2024-01-01').isLeapYear(); // true
moment('2023-01-01').isLeapYear(); // false
Is a Moment ObjectChecks if a variable is a Moment.js object.
moment.isMoment(obj);
moment.isMoment(moment()); // true
moment.isMoment(new Date()); // false
Is a Native Date ObjectChecks if a variable is a native JavaScript Date object.
moment.isDate(obj);
moment.isDate(new Date()); // true
moment.isDate(moment()); // false
const eventStart = moment('2024-03-15');
const eventEnd = moment('2024-03-20');
const userSignupDate = moment('2024-03-18');
const today = moment();
if (userSignupDate.isBetween(eventStart, eventEnd, 'day', '[]')) {
console.log('User signed up during the event.');
} else {
console.log('User did not sign up during the event.');
}
if (today.isAfter(eventEnd)) {
console.log('The event has finished.');
}
if (moment('2024-02-29').isValid() && moment('2024-01-01').isLeapYear()) {
console.log('2024 is a leap year, and Feb 29th is a valid date.');
}

These query methods are essential building blocks for date-related logic in applications.

Explore the documentation for more nuances: https://momentjs.com/docs/#/query/