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.
Date Comparison Methods
Section titled “Date Comparison Methods”These methods allow you to compare one moment relative to another:
| Method | Description & Syntax |
|---|---|
| Is Before | Checks 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'); // truemoment('2023-10-26').isBefore('2023-10-26', 'day'); // false (same day) |
| Is Same | Checks if a moment is the same as another moment.moment().isSame(Moment|String|Number|Date|Array);moment().isSame(Moment|String|..., String); // Optional: specify unit granularitymoment('2023-10-26').isSame('2023-10-26'); // truemoment('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 After | Checks if a moment is after another moment.moment().isAfter(Moment|String|Number|Date|Array);moment().isAfter(Moment|String|..., String); // Optional: specify unit granularitymoment('2023-11-01').isAfter('2023-10-26'); // true |
| Is Same or Before | Checks 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 granularitymoment('2023-10-26').isSameOrBefore('2023-10-26'); // truemoment('2023-10-25').isSameOrBefore('2023-10-26', 'day'); // true |
| Is Same or After | Checks 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 granularitymoment('2023-10-26').isSameOrAfter('2023-10-26'); // truemoment('2023-10-27').isSameOrAfter('2023-10-26', 'day'); // true |
| Is Between | Checks if a moment is between two other moments.moment().isBetween(momentLikeStart, momentLikeEnd);moment().isBetween(start, end, String); // Optional: specify unit granularitymoment().isBetween(start, end, String, String); // Optional: inclusivity '()', '[)', '(]', '[]'moment('2023-10-26').isBetween('2023-10-01', '2023-10-31'); // truemoment('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) |
Date Property Queries
Section titled “Date Property Queries”These methods check specific characteristics of a moment or a variable:
| Method | Description & Syntax |
|---|---|
| Is Daylight Saving Time | Checks 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 Year | Checks if the moment’s year is a leap year.moment('2024-01-01').isLeapYear(); // truemoment('2023-01-01').isLeapYear(); // false |
| Is a Moment Object | Checks if a variable is a Moment.js object.moment.isMoment(obj);moment.isMoment(moment()); // truemoment.isMoment(new Date()); // false |
| Is a Native Date Object | Checks if a variable is a native JavaScript Date object.moment.isDate(obj);moment.isDate(new Date()); // truemoment.isDate(moment()); // false |
Example: Checking Event Eligibility
Section titled “Example: Checking Event Eligibility”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/