title: Javascript - Best Way to Compare Dates
body: <p>I would like to gather everyone's opinion on what is the correct way to sort dates in Javascript. </p>
  
  <p>Review the below code snippets, I have 2 scenario to sort the dates using: <br/></p>
  
  <p>i) javascript date object  <br/>
  ii) custom javascript date string format</p>
  
  <p>Can I know why when I sort by custom javascript date format, the final results display seems to be wrong compare to the sorting using javascript date objects. </p>
  
  <p>Should I sort the date using javascript date objects?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>Date.prototype.getDateFormat = function (value) {
          var d = this.getDate();
          if (d &lt; 10)
              d = "0" + d;
  
          var month = this.getMonth() + 1;
          if (month &lt; 10)
              month = "0" + month;
  
          return value.replace("MM", month).replace("dd", d).replace("yyyy", this.getFullYear());
  }
  
  Date.prototype.getTimeFormat = function (value) {
          var h = this.getHours();
          var m = this.getMinutes();
          var s = this.getSeconds();
          var a = "AM";
  
          if (h == 12) {
              a = "PM";
          } else if (h &gt; 12) {
              a = "PM";
              h = h - 12;
          }
          if (h &lt; 10) {
              h = "0" + h;
          }
          if (m &lt; 10) {
              m = "0" + m;
          }
          if (s &lt; 10) {
              s = "0" + s;
          }
          return value.replace("hh", h).replace("mm", m).replace("ss", s).replace("tt", a);
  }
  
  function sortDateTime(a,b){
   if(a &gt; b) return 1;
    else if(a &lt; a) return -1;
  }
  
  var dateList1 = [];
  var dateList2 = [];
  
  // Using pure Javascript Date Object
  var date1 = new Date("06/03/2017 12:00:00 PM");
  var date2 = new Date("06/03/2017 03:00:00 PM");
  var date3 = new Date("06/03/2017 05:00:00 PM");
  
  dateList1.push(date1);
  dateList1.push(date2);
  dateList1.push(date3);
  
  var result1 = dateList1.sort(sortDateTime);
  
  for(var e = 0 ; e &lt; result1.length ; e++){
    var display = "&lt;div&gt;" + result1[e] + "&lt;/div&gt;";
    $("#result1").append(display);
  }
  //Result from result1 List
  // 1) 06/03/2017 12:00:00 PM
  // 2) 06/03/2017 03:00:00 PM 
  // 3) 06/03/2017 05:00:00 PM
  
  // Using Javascript Date Time custom string format
  var pDate1 = new Date("06/03/2017 12:00:00 PM");
  var pDate2 = new Date("06/03/2017 03:00:00 PM");
  var pDate3 = new Date("06/03/2017 05:00:00 PM");
  
  var rDate1 = pDate1.getDateFormat("yyyyMMdd") + pDate1.getTimeFormat(" tt hhmmss");
  var rDate2 = pDate2.getDateFormat("yyyyMMdd") + pDate2.getTimeFormat(" tt hhmmss");
  var rDate3 = pDate3.getDateFormat("yyyyMMdd") + pDate3.getTimeFormat(" tt hhmmss");
  
  dateList2.push(rDate1);
  dateList2.push(rDate2);
  dateList2.push(rDate3);
  
  var result2 = dateList2.sort(sortDateTime);
  
  for(var e = 0 ; e &lt; result2.length ; e++){
    var display = "&lt;div&gt;" + result2[e] + "&lt;/div&gt;";
    $("#result2").append(display);
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;b&gt; Sort by Javascript Date object&lt;/b&gt;
  &lt;div id="result1"&gt;&lt;/div&gt;
  &lt;br/&gt;
  &lt;b&gt; Sort by Javascript Custom Date Time string Format&lt;/b&gt;
  &lt;div id="result2"&gt;&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>