JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

How to check if a date is before today in JavaScript?

To check if a date is before today, compare today's date with the Date object, date1 < today. If the comparison is true, it means the date is before today; otherwise, date is today's date or a date after today.

let today  = new Date();
let date1 = new Date(2022, 0, 27); //27 January, 2022

if(date1 < today){
  console.log("date1 is before today's date.");
  console.log("date1 is equal to or after today's date.");


"date1 is before today's date."

Today's Date object is created by passing nothing to the Date() constructor.

let today = new Date();
console.log(today); //Sun Oct 02 2022 18:38:54 GMT+0530 (India Standard Time)

At the time of writing this tutorial, today's date is 02 October 2022.

You can create a Date object in two ways:

  1. You pass a valid date string to the Date() constructor.

    let d = new Date('02-10-2022');
  2. You pass comma-separated arguments in a correct order: year, month, date, hours, minutes, and seconds to the Date() constructor.

    let d = new Date(2022, 9, 2, 10, 30, 00);
    console.log(d); //Sun Oct 02 2022 10:30:00 GMT+0530 (India Standard Time)

You might be thinking, how is comparison performed between two Date objects?

Actually, when you compare two Date objects, each Date object is converted to its timestamp value. Timestamp is the number of milliseconds since midnight on 1 January 1970. You can get the timestamp by calling the getTime() method.

let d = new Date(2022, 9, 2, 10, 30, 00);
console.log(d.getTime()); //1664686800000

Recommended Posts