How to compare two JavaScript Date Objects?

JavaScript Date objects can be compared directly using comparison operators (>, <, >=, <=) or by converting them to timestamps for equality checks.

Direct Comparison with Operators

You can compare dates directly using comparison operators, which automatically convert dates to their numeric values (milliseconds since epoch):

<!DOCTYPE html>
<html>
  <body>
    <script>
      var date1, date2;
      date1 = new Date();
      document.write("Current date: " + date1);
      
      date2 = new Date("Dec 10, 2015 20:15:10");
      document.write("<br>Custom date: " + date2);
      
      if (date1 > date2) {
        document.write("<br>Current date is more recent.");
      } else {
        document.write("<br>Custom date is more recent.");
      }
    </script>
  </body>
</html>
Current date: Mon May 28 2018 09:48:49 GMT+0530 (India Standard Time)
Custom date: Thu Dec 10 2015 20:15:10 GMT+0530 (India Standard Time)
Current date is more recent.

Comparing for Equality

For exact equality, use getTime() method since direct equality (==) compares object references, not values:

<!DOCTYPE html>
<html>
  <body>
    <script>
      var date1 = new Date("2023-01-15");
      var date2 = new Date("2023-01-15");
      var date3 = new Date("2023-01-16");
      
      // Direct comparison (incorrect for equality)
      document.write("date1 == date2: " + (date1 == date2));
      document.write("<br>");
      
      // Using getTime() for equality (correct)
      document.write("date1.getTime() === date2.getTime(): " + (date1.getTime() === date2.getTime()));
      document.write("<br>");
      document.write("date1.getTime() === date3.getTime(): " + (date1.getTime() === date3.getTime()));
    </script>
  </body>
</html>
date1 == date2: false
date1.getTime() === date2.getTime(): true
date1.getTime() === date3.getTime(): false

All Comparison Methods

Here's a complete example showing all comparison operations:

<!DOCTYPE html>
<html>
  <body>
    <script>
      var earlier = new Date("2023-01-15");
      var later = new Date("2023-01-20");
      var same = new Date("2023-01-15");
      
      document.write("Earlier: " + earlier);
      document.write("<br>Later: " + later);
      document.write("<br>Same: " + same);
      document.write("<br><br>");
      
      // Greater than / Less than
      document.write("later > earlier: " + (later > earlier));
      document.write("<br>earlier < later: " + (earlier < later));
      document.write("<br>");
      
      // Greater than or equal / Less than or equal
      document.write("earlier >= same: " + (earlier >= same));
      document.write("<br>earlier <= same: " + (earlier <= same));
      document.write("<br>");
      
      // Equality using getTime()
      document.write("earlier.getTime() === same.getTime(): " + (earlier.getTime() === same.getTime()));
    </script>
  </body>
</html>
Earlier: Sun Jan 15 2023 00:00:00 GMT+0530 (India Standard Time)
Later: Fri Jan 20 2023 00:00:00 GMT+0530 (India Standard Time)
Same: Sun Jan 15 2023 00:00:00 GMT+0530 (India Standard Time)

later > earlier: true
earlier < later: true
earlier >= same: true
earlier <= same: true
earlier.getTime() === same.getTime(): true

Key Points

  • Use >, <, >=, <= for direct date comparisons
  • Use getTime() method for equality checks between dates
  • Direct equality (== or ===) compares object references, not date values
  • Comparison operators automatically convert dates to milliseconds for comparison

Conclusion

JavaScript Date objects support direct comparison using standard operators for chronological ordering. For equality checks, always use getTime() to compare the actual date values rather than object references.

Updated on: 2026-03-15T23:18:59+05:30

415 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements