尝试按降序对时间进行排序



我正在尝试按降序对退出汽车的时间进行排序。例:

这只是我正在使用的数据的一部分。实际数据包括 100 多个数据。

我尝试使用 .sort((a, b( => b - a(打开,let outDate = new Date(car.out((这可能不起作用,因为它也在对日期进行排序,不确定(,hoursOut.sort((a, b( => b - a( 这也不起作用。收到未收到函数错误

提前感谢!



function displayData(data) {
data.forEach(car =>{
// -------------------------------------------- Time in --------------
let dateIn = new Date(car.in)
let month = dateIn.getMonth()
let date = dateIn.getDate();
let fullYear = dateIn.getFullYear()
let hoursIn = dateIn.getHours();
let minutesIn = dateIn.getMinutes();
let secondsIn = dateIn.getSeconds()
let timeInFormat = hoursIn >= 12 ? 'PM' : 'AM';
let timeIn = month + '/' + date  + '/' + fullYear + ' ' + hoursIn + ':' + minutesIn + ':' + secondsIn + " " + timeInFormat
//input: 1591855348009
//output: 5/11/2020 16:47:48 PM
//-------------------------------------------------- Time out --------------------------------------------
// let x = car.out
// let carsOut = x.sort((a, b) => b - a)
let outDate = new Date(car.out);
// let sortedList = outDate.sort((a, b) => b.date - a.date)
let monthOut = outDate.getMonth()
let dateOut = outDate.getDate();
let yearOut = outDate.getFullYear()
let hoursOut = outDate.getHours();
let minutesOut = outDate.getMinutes();
let secondsOut = outDate.getSeconds()
let timeOutFormat = hoursOut >= 12 ? 'PM' : 'AM';
let timeOut = monthOut + '/' + dateOut  + '/' + yearOut + ' ' + hoursOut + ':' + minutesOut + ':' + secondsOut + " " + timeOutFormat

// ------------------------------------------ Duration
let duration = ((Math.abs(dateIn - outDate))/3600000).toFixed(2)
if (duration > 1) {
var price = (duration * 2.99).toFixed(2);
}else if (duration >= 24) {
// change the font color to red
}
else{
var price = 0;
// change the font color to blue
price.style.fontcolor = 'blue'    // not working
// price.fontcolor("blue");
}
table.innerHTML += `<tr id=${car.id}>
<td>${car.license}</td>
<td>${price}</td>
<td>${duration}</td>        
<td>${timeIn}</td>
<td>${timeOut}</td>
</tr>`
// table.innerHTML += row
});
}

@Edit

data.sort((a, b) => b.out - a.out).forEach(car =>{
// -------------------------------------------- Time in --------------
let dateIn = new Date(car.in)
let month = dateIn.getMonth()
let date = dateIn.getDate();
let fullYear = dateIn.getFullYear()
let hoursIn = dateIn.getHours();
let minutesIn = dateIn.getMinutes();
let secondsIn = dateIn.getSeconds()
let timeInFormat = hoursIn >= 12 ? 'PM' : 'AM';
hoursIn = hoursIn % 12
let timeIn = month + '/' + date  + '/' + fullYear + ' ' + hoursIn + ':' + minutesIn + ':' + secondsIn + " " + timeInFormat
//input: 1591855348009
//output: 5/11/2020 16:47:48 PM
//-------------------------------------------------- Time out --------------------------------------------
// let x = car.out
// let carsOut = x.sort((a, b) => b - a)
let outDate = new Date(car.out);
// let sortedList = outDate.sort((a, b) => b.date - a.date)
let monthOut = outDate.getMonth()
let dateOut = outDate.getDate();
let yearOut = outDate.getFullYear()
let hoursOut = outDate.getHours();
let minutesOut = outDate.getMinutes();
let secondsOut = outDate.getSeconds()
let timeOutFormat = hoursOut >= 12 ? 'PM' : 'AM';
let timeOut = monthOut + '/' + dateOut  + '/' + yearOut + ' ' + hoursOut + ':' + minutesOut + ':' + secondsOut + " " + timeOutFormat
//input: 1591855348009
//output: 5/11/2020 16:47:48 PM
// ------------------------------------------ Duration
let duration = ((Math.abs(dateIn - outDate))/3600000).toFixed(2)
if (duration > 1) {
var price = (duration * 2.99).toFixed(2);
}else if (duration >= 24) {
// change the font color to red
}
else{
var price = 0;
// change the font color to blue
price.style.fontcolor = 'blue'    // not working
// price.fontcolor("blue");
}
table.innerHTML += `<tr id=${car.id}>
<td>${car.license}</td>
<td>${price}</td>
<td>${duration}</td>        
<td>${timeIn}</td>
<td>${timeOut}</td>
</tr>`
// table.innerHTML += row
});
}

最新更新