title: Compare two dates in JavaScript
body: <p>I am trying to compare two dates. I have this code which I thought would work a treat, but it didn't. For now I just want to alert with an error if the end date is less than the start date. The date style, <code>yyyy-mm-dd</code>, needs to be kept in this format for other events prior to this. What is wrong with this code?</p>
  
  <pre><code>   startdate = "2009-11-01" ;
     enddate  = "2009-11-04" ;
  
     var d1 = new Date(startdate)
     var d2 = new Date(enddate)
  
     if (d2 &lt; d1) {
        alert ("Error ! ) ;
     }
  
     document.cookie='st =' + startdate // set sytem cookie
     document.cookie='en =' + enddate
     window.location = self.location.href
     window.opener.location.reload()
     close()
  </code></pre>