Compare Date with Current Date in JavaScript

Compare Date with Current Date in JavaScript

To compare a date with the current date in JavaScript, you can use the Date object and the getDate method.

Here's an example of how to compare a given date with the current date using the getDate method:

const givenDate = new Date("2022-12-31");
const currentDate = new Date();

if (givenDate.getDate() > currentDate.getDate()) {
  console.log("Given date is in the future");
} else if (givenDate.getDate() < currentDate.getDate()) {
  console.log("Given date is in the past");
} else {
  console.log("Given date is the current date");
}

In this example, we again create two Date objects: one for the given date and one for the current date. Then we use the getDate method to get the day of the month (1-31) for each date. Finally, we use an if-else statement to compare the two dates based on their day of the month. If the day of the

Related: