Really not sure where I'm going wrong here.
I have some JavaScript to sort a table by date value:
function sortByDate() {
if (jQuery("#web-orders .data-table tbody").length > 0) {
var tbody = document.querySelector("#web-orders .data-table tbody");
var rows = [].slice.call(tbody.querySelectorAll("tr"));
}
if (jQuery("#store-orders .data-table tbody").length > 0) {
var tbodyStore = document.querySelector("#store-orders .data-table tbody");
var rowsStore = [].slice.call(tbodyStore.querySelectorAll("tr"));
rowsStore.forEach(function (entry) {
rows.push(entry);
});
}
rows.sort(function (a, b) {
console.log("a.cells[2].innerHTML = " + a.cells[2].innerHTML);
console.log("b.cells[2].innerHTML = " + b.cells[2].innerHTML);
a = new Date(Date.parse(a.cells[2].innerHTML));
b = new Date(Date.parse(b.cells[2].innerHTML));
console.log("a = " + a);
console.log("b = " + b);
return a - b;
});
rows.forEach(function (v) {
tbody.appendChild(v); // note that .appendChild() *moves* elements
});
}
Now here is some of the console output with the invalid dates:
a.cells[2].innerHTML = 28/11/2017 1:49:37 PM
b.cells[2].innerHTML = 5/09/2017 6:27:35 AM
a = Invalid Date
b = Tue May 09 2017 06:27:35 GMT+0930 (Cen. Australia Standard Time)
a.cells[2].innerHTML = 28/11/2017 1:49:37 PM
b.cells[2].innerHTML = 24/09/2017 6:12:48 PM
a = Invalid Date
b = Invalid Date
Does anyone know why this might be happening? It's got me stumped.