check if date is less than today moment

How to Check if a Date is Less Than Today Moment

If you want to check whether a given date is less than today's date, you can use JavaScript's built-in Date object.

Method 1: Using getTime() method of Date object

The getTime() method returns the number of milliseconds between the specified date and midnight January 1 1970. By comparing this value to the current date's getTime() value, we can determine if the specified date is less than the current date.


let givenDate = new Date('2022-06-10');
let currentDate = new Date();
if (givenDate.getTime() < currentDate.getTime()) {
    console.log('Given date is less than current date.');
} else {
    console.log('Given date is not less than current date.');
}

Method 2: Using comparison operators

You can directly compare two Date objects using comparison operators like less than (<) or greater than (>). When you use these operators, JavaScript internally calls the valueOf() method of the Date object which returns the number of milliseconds between the specified date and midnight January 1 1970.


let givenDate = new Date('2022-06-10');
let currentDate = new Date();
if (givenDate < currentDate) {
    console.log('Given date is less than current date.');
} else {
    console.log('Given date is not less than current date.');
}

Both methods give the same output, but the second method is easier to read and write. You can choose the method that works best for you.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe