Compare two dates with JavaScript – Examples

Compare two dates with JavaScript – Examples

Compare two dates with JavaScript. Here we will learn, how to compare two dates with javascript. Learn different techniques to compare 2 dates in javascript with examples.

Compare two dates with JavaScript

In this tutorial, we will take several examples for comparing two dates in javascript with time, without time and date and current date.

First Method – Compare two dates with JavaScript

You can use the below javascript code for comparing two dates without time:

   var date1 = new Date('2019-06-27');
   var date2 = new Date('2019-05-25');
    if(date1>date2){
      document.write('date1>date2');
    } else if(date1<date2) {
       document.write('date1<date2');
    }
    else{
      document.write('false');
    }

Ex:-

<!DOCTYPE html>
<html>
<head>
  <title>Compare two dates javascript</title>
</head>
<body>
  <script type="text/javascript">
    var date1 = new Date('2019-06-27');
    var date2 = new Date('2019-05-25');
    if(date1>date2){
      document.write('date1>date2');
    } else if(date1<date2) {
       document.write('date1<date2');
    }
    else{
      document.write('false');
    }
  </script>
</body>
</html>

Result of the above code is: date1>date2

Second Method – compare date with current date in javascript

Let’s take the second example for compare date with the current date in javascript:

    var currentDate = new Date();
    var date = new Date('2019-12-28');

    if(currentDate>date){
      document.write('currentDate > date');
    }else if(currentDate<date) {
       document.write('currentDate < date');
    }
    else{
      document.write('currentDate==date');
    }

Ex:-

<!DOCTYPE html>
<html>
<head>
  <title>compare date with current date in javascript</title>
</head>
<body>
  <script type="text/javascript">
    var currentDate = new Date();
    var date = new Date('2019-12-28');

    if(currentDate>date){
      document.write('currentDate > date');
    }else if(currentDate<date) {
       document.write('currentDate < date');
    }
    else{
      document.write('currentDate==date');
    }
  </script>
</body>
</html>

Result of the above code is: currentDate < date

Third method – javascript compare dates with time

Let’s take an example to compare two dates with time:

    var date1 = new Date( "Dec 18, 2019 20:40:45" );
    var date2 = new Date( "Nov 15, 2018 21:35:40" );

    if(date1>date2){
      document.write('date1 > date2');
    }else if(date1<date2) {
       document.write(' date1 < date2 ');
    }
    else{
      document.write(' date1 == date2 ');
    }

Ex:-

<!DOCTYPE html>
<html>
<head>
  <title>javascript compare dates with time</title>
</head>
<body>
  <script type="text/javascript">
    var date1 = new Date( "Dec 18, 2019 20:40:45" );
    var date2 = new Date( "Nov 15, 2018 21:35:40" );

    if(date1>date2){
      document.write('date1 > date2');
    }else if(date1<date2) {
       document.write(' date1 < date2 ');
    }
    else{
      document.write(' date1 == date2 ');
    }
  </script>
</body>
</html>

Result of the above code is: date1 > date2

Fourth Method – javascript compare two dates with time

Here we will take example to compare two dates using the getTime() method:

      var d1 = new Date( " Wed Nov 26 2019 09:52:06 " ); 
      var d2 = new Date(); 
      document.write("date 1 is :- " + d1); 
      document.write("<br>"); 
      document.write("date 2 is :- " + d2); 
      document.write("<br>"); 
      if (d1.getTime() === d2.getTime()) 
          document.write("Both  are equal"); 
      else
          document.write("Not equal"); 

Ex:-

      var d1 = new Date( " Wed Nov 26 2019 09:52:06 " ); 
      var d2 = new Date(); 
      document.write("date 1 is :- " + d1); 
      document.write("<br>"); 
      document.write("date 2 is :- " + d2); 
      document.write("<br>"); 
      if (d1.getTime() === d2.getTime()) 
          document.write("Both  are equal"); 
      else
          document.write("Not equal"); 

Result of above code is:

date 1 is :- Tue Nov 26 2019 09:52:06 GMT+0530 (India Standard Time)
date 2 is :- Wed Nov 27 2019 09:52:50 GMT+0530 (India Standard Time)
Not equal

If you want to know more about javascript date and time methods, you may like following date and time methods:

You may like

  1. JavaScript Get Month Name With Various Examples
  2. Get Month 2 Digits in JavaScript
  3. javaScript Get Current Year 2 and 4 Digit – Example
  4. Get Current Date Time javaScript
  5. JavaScript: Date setUTCDate() Method
  6. Set UTC Month In javaScript
  7. setUTCFullYear() Method JavaScript With Examples
  8. javascript date setUTCHours() Method With Examples
  9. JavaScript: d.setUTCMinutes() Method e.g.
  10. setUTCSecond() Method By JavaScript
  11. javaScript Date set UTC milliseconds
  12. setUTCSecond() Method By JavaScript
  13. JavaScript: Date.getUTCDate() Method
  14. getUTCmonth Method javaScript With Example
  15. Digital Clock with date in javaScript
  16. JavaScript: Set Date Methods
  17. JavaScript Get Date Methods

If you have any questions or thoughts to share, use the comment form below to reach us.

Leave a Reply

Your email address will not be published. Required fields are marked *