//判断时间
let today = [] as any
let yesterday = [] as any
let aWeek = [] as any
let aMonth = [] as any
let longlongAgo = [] as any
//封装时间判断方法
let judgeTime = function (time) {
let date = time
let oneDay = 60 * 60 * 24 //date = 1684119095 //2023/5/16
date = date - (date % oneDay)
let nowDate = Date.parse(new Date()) / 1000
nowDate = nowDate - (nowDate % oneDay)
// console.log(nowDate,'now')
// console.log(date)
let checkTime = (nowDate - date) / oneDay
console.log(checkTime,'checkTime')
if (checkTime < 0) {
// console.log('未来')
} else if (checkTime == 0) {
// console.log('今天')
today.push(element)
} else if (checkTime > 0 && checkTime <= 1) {
// console.log('昨天')
yesterday.push(element)
} else if (checkTime > 1 && checkTime <= 7) {
// console.log('一周内')
aWeek.push(element)
} else if (checkTime > 7 && checkTime <= 30) {
aMonth.push(element)
// console.log('30天内')
} else if (checkTime > 30) {
longlongAgo.push(element)
// console.log('很久以前')
}
}
//定义一个空数组,用于组装二维数组
const allConversations=ref([])
//在方法中使用
function initData(data) {
today = []
yesterday = []
aWeek = []
aMonth = []
longlongAgo = []
for (let index = 0; index < data.length; index++) {
const element = data[index]
//使用判断时间
judgeTime(element.lastActiveTime)
}
nextTick(() => {
allConversations.value = [today, yesterday, aWeek, aMonth, longlongAgo]
//排序
allConversations.value.forEach((item, index) => {
item.sort((a, b) => {
return a.lastActiveTime - b.lastActiveTime
})
})
console.log(allConversations.value, 'allConversations.value')
})
}
此时就可以将组装好的allConversations用以分时间渲染了