Check If a Date is Today or in the Future in Moment JS
Last Updated :
26 Jul, 2024
Moment.js is the JavaScript library that is capable of parsing, validating, manipulating, and formatting dates and times. It provides a range of methods to perform date comparisons, such as checking if a date is today or in the future. Using methods like isSame, isAfter, isBefore, and diff, you can easily determine the relative position of a date with respect to the current date.
Run the below command before running the code in your local system:
npm i moment
These are the following approaches we are going to discuss:
Using isSame() and isAfter() Methods
In this approach, we are using the isSame() method to check if dateToCheck is the same as today's date when compared by day. Additionally, we use the isAfter() method to determine if dateToCheck is in the future relative to today. If either condition is true, it indicates that the date is today or in the future.
Syntax:
momentObject.isSame(momentObjectOrValue, [unit])
momentObject.isAfter(momentObjectOrValue, [unit])
Example: The below example uses isSame and isAfter Methods to Check if a date is today or in the future in Moment JS.
JavaScript
//script.js
const moment = require('moment');
let dateToCheck = moment('2024-07-26');
let today = moment().startOf('day');
if (dateToCheck.isSame(today, 'day') || dateToCheck.isAfter(today)) {
console.log('The date is today or in the future.');
} else {
console.log('The date is in the past.');
}
Output:
The date is today or in the future.
Using isBefore() Method
In this approach, we are using the isBefore() method to check if dateToCheck is before today's date when compared by day. If dateToCheck is before today, it indicates that the date is in the past. If not, the date is either today or in the future.
Syntax:
momentObject.isBefore(momentObjectOrValue, [unit])
Example: The below example uses isBefore Method to Check if a date is today or in the future in Moment JS.
JavaScript
//script.js
const moment = require('moment');
let dateToCheck = moment('2024-07-26');
let today = moment().startOf('day');
if (dateToCheck.isBefore(today, 'day')) {
console.log('The date is in the past.');
} else {
console.log('The date is today or in the future.');
}
Output:
The date is today or in the future.
Using diff() Method
In this approach, we are using the diff() method to calculate the difference in days between dateToCheck and today. If the difference in days is greater than or equal to 0, it indicates that the date is either today or in the future. If the difference is negative, the date is in the past.
Syntax:
momentObject.diff(momentObjectOrValue, [unit], [absolute])
Example: The below example uses diff Method to Check if a date is today or in the future in Moment JS.
JavaScript
// script.js
const moment = require('moment');
let dateToCheck = moment('2024-07-26');
let today = moment().startOf('day');
let diffInDays = dateToCheck.diff(today, 'days');
if (diffInDays >= 0) {
console.log('The date is today or in the future.');
} else {
console.log('The date is in the past.');
}
Output:
The date is today or in the future.
Similar Reads
How to Check the Input Date is Equal to Today's Date or not using JavaScript? To check if an input date is equal to today's date in JavaScript, you can compare the input date with the current date by creating Date objects for both and checking if they match. This comparison typically involves comparing the year, month, and day values of both dates.Below are the approaches to
3 min read
How to use moment.js to change date format in jQuery ? Using Moment.js to change date format in jQuery involves importing the Moment.js library and utilizing its functions to parse, manipulate, and format dates. You can easily convert dates into various formats by calling moment(date).format(desiredFormat) to display the date in your preferred format.mo
2 min read
How To Compare Only Date In Moment.js? In Moment.js, date manipulation and parsing can be done efficiently. Along with this, the library provides ways to compare only the date portion of two date objects, ignoring the time component. Methods like startOf, isSame, and formatting techniques enable you to focus solely on the date part for a
2 min read
Check Whether the Current Time is Between 2 Times in Moment.JS? In Moment.js, checking whether the current time falls between two specified times involves comparing the current moment against the two given times. This allows you to determine if the present time is within a defined time range, making it useful for tasks like scheduling or time-based conditions.In
3 min read
How to to Get Number of Days in Current Month in Moment.JS? While working with dates in JavaScript, sometimes we need to find the number of days in the current month. For example, February has 28 or 29 days depending on whether it's a leap year or not, while other months have 30 or 31 days. Moment.js is a powerful JavaScript library that makes handling dates
2 min read