How to check if date is in the past or future in JavaScript

How to check if date is in the past or future in JavaScript

Posted by Ervin Adams on July 27, 2022

How to check if date is in the past or future in JavaScript

How to check if date is in the past or future in JavaScript

Posted by Ervin Adams on July 27, 2022

Here is how you can check if a date is in the past or future in JavaScript:

Step 1. Convert the date that you want to check using date() in milliseconds

let day = new Date('2022-07-05');

Step 2. Get the current date using date() in milliseconds

let current = Date();

Step 3. Now that the current day and the day to be checked are in milliseconds, check if day is greater than current

day > current

If the result is true, you have a future date. If it is false the date is in the past.

Here is a full example:

function checkDate(result) {
  const current = new Date();

  return  result > current;
}

let day = new Date('2022-07-05'); //false

let day1 = new Date('2022-08-05'); //true

console.log(checkDate(day));

console.log(checkDate(day1));