话不多说直接上代码,菜鸟一只,有误请指教,感谢!
function getHoursList () {
let date = new Date()
let startCount = date.getHours() + 1 // date.getHours() // 获取当前小时
let dateType = '今天'
let hoursCount = 24
let hoursList = []
for (let i = 0; i <= hoursCount - 1; i++) {
console.log(i)
if (startCount === 0) {
startCount = 24
dateType = '昨天'
}
startCount = startCount - 1
hoursList.push({ date: dateType, hours: startCount >= 10 ? startCount + '时' : '0' + startCount + '时' })
}
return hoursList
}
let hoursListArr = getHoursList ()
console.log(hoursListArr)