Open In App

Check If a Date is Today or in the Future in Moment JS

Last Updated : 26 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Next Article

Similar Reads