Skip to content
the poor coder

check if date is less than today moment

Poor Man
Published
Reading time
1 min read
On this page

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.

Text

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.

Text

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC