Javascript - Best Way to Compare Dates

Viewed 3438

I would like to gather everyone's opinion on what is the correct way to sort dates in Javascript.

Review the below code snippets, I have 2 scenario to sort the dates using:

i) javascript date object
ii) custom javascript date string format

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.

Should I sort the date using javascript date objects?

Date.prototype.getDateFormat = function (value) {
        var d = this.getDate();
        if (d < 10)
            d = "0" + d;

        var month = this.getMonth() + 1;
        if (month < 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 > 12) {
            a = "PM";
            h = h - 12;
        }
        if (h < 10) {
            h = "0" + h;
        }
        if (m < 10) {
            m = "0" + m;
        }
        if (s < 10) {
            s = "0" + s;
        }
        return value.replace("hh", h).replace("mm", m).replace("ss", s).replace("tt", a);
}

function sortDateTime(a,b){
 if(a > b) return 1;
  else if(a < 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 < result1.length ; e++){
  var display = "<div>" + result1[e] + "</div>";
  $("#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 < result2.length ; e++){
  var display = "<div>" + result2[e] + "</div>";
  $("#result2").append(display);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<b> Sort by Javascript Date object</b>
<div id="result1"></div>
<br/>
<b> Sort by Javascript Custom Date Time string Format</b>
<div id="result2"></div>

1 Answers
Related