[RESOLVED]compare two dates in javascript

Hi all,

          How we compare  below mentioned dates in javascript?

November 13, 2014 and November 25, 2014

var x = new Date(‘November 13, 2014′);
var y = new Date(‘November 25, 2014′);
alert(x < y);

You can use the Date.Parse method to convert the value to date object and then compare it.

function CompareDate()
    {
        //Parse the date to date object here
        var date1 = Date.parse("November 13, 2014 ");
        var date2 = Date.parse("November 25, 2014");
      //Compare the dates
      if(date1 < date2)
        {
          alert("Date 1 is less than Date 2");
        }
    }

Complete Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script>
  function CompareDate()
    {
        //Parse the date to date object here
        var date1 = Date.parse("November 13, 2014 ");
        var date2 = Date.parse("November 25, 2014");
      //Compare the dates
      if(date1 < date2)
        {
          alert("Date 1 is less than Date 2");
        }
    }
  </script>
</head>
<body>
    <input type="button" Value="Button1" onclick="CompareDate()" />
</body>
</html>

Leave a Reply