How to compare two JavaScript Date Objects?


To compare two date objects with JavaScript, create two dates object and get the recent date to compare it with the custom date.

Example

You can try to run the following code to compare two dates −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         var date1, date2;
         date1 = new Date();
         document.write(date1);
         date2 = new Date( "Dec 10, 2015 20:15:10" );
         document.write("<br>"+date2);
         if (date1 > date2) {
            document.write("<br>Date1 is the recent date.");
         } else {
            document.write("<br>Date 2 is the recent date.");
         }
      </script>
   </body>
</html>

Output

Mon May 28 2018 09:48:49 GMT+0530 (India Standard Time)
Thu Dec 10 2015 20:15:10 GMT+0530 (India Standard Time)
Date1 is the recent date

Updated on: 18-Jun-2020

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements