How to compare dates in JavaScript?


Dates can be compared easily in the JavaScript the dates can belong to any frame like past, present, or future. Past dates can be compared to the future and the future can be compared to the past and present.

The date object will do what you want to construct one for each, then compare them using the ( >,<,<= or >= ). The ==, !=, === and !== operators require you to use date.getTime().

In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the data into a numeric value by using the getTime() method by converting the given dates into numeric values we can directly compare them.

Example 1

In the following example, we are comparing the dates present date to provided date.

<!DOCTYPE html>
<html>
<head>
   <title>Compare the date with other date</title>
</head>
<body>
   <p id="compare"></p>
   <script>
      var today = new Date();
      var otherday = new Date();
      otherday.setFullYear(2022, 01, 19);
      if (otherday > today) {
         var date = "The date you provide is a future date";
      } else {
         var date = "The date you provide is a past date";
      }
      document.getElementById("compare").innerHTML = date;
   </script>
</body>
</html>

Example 2

In the following example, illustrate the comparison between dates with the help of the getTime() function.

<!DOCTYPE html>
<html>
<head>
   <title>Compare the date with other date</title>
</head>
<body>
   <p id="compare"></p>
   <script>
      var d1 = new Date();
      var d2 = new Date();
      if (d1.getTime() === d2.getTime()) document.write("Both are equal");
      else document.write("Not equal");
   </script>
</body>
</html>

Example 3

In the following example, the JavaScript code illustrates the comparison of two given dates.

<!DOCTYPE html>
<html>
<head> </head>
<body>
   <script>
      var d1 = new Date(2019, 08, 03, 11, 45, 55); // (YYYY, MM, DD, Hr, Min, Sec)
      var d2 = new Date(2019, 08, 03, 11, 45, 55);
      if (d1.getTime() < d2.getTime())
         document.write("d1 is lesser than d2");
      else if (d1.getTime() > d2.getTime())
         document.write("d1 is greater than d2");
      else document.write("both are equal");
   </script>
</body>
</html>

Example 4

In the following, we are comparing two dates but the logic behind this is day1 is automatically taking the current dates and on day2 we are passing the date.

Note − if we are passing the month value then it will be month value − 1 for example − if you thinking to pass the January month then the value will be 1−1 = 0, February value will be 1, etc. directly we can say that it is taking the value like an index of the array, 0,1,2,3,4….

<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8" />
   <meta http-equiv="X-UA-Compatible" content="IE=edge" />
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   <title>date compare</title>
</head>
<body>
   <script>
      var day1 = new Date();
      day1.setHours(0, 0, 0, 0);
      var day2 = new Date(2022, 06, 20);
      day2.setHours(0, 0, 0, 0); //here we are stoping the clock
      document.write("day1=>", day1);
      document.write("<br \>day2=>", day2);
      if (day1 > day2) {
         document.write("<br \>day1 is greater than day2");
      } else if (day1 < day2) {
         document.write("<br \>day1 is older than day2");
      } else {
         document.write("<br \>day1 and day2 are same");
      }
   </script>
</body>
</html>

Updated on: 06-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements