判断一维对象数组的对象时间属性值是未来、今天、昨天、一周内、30天内、30天以前,并将该数组按照时间分类组成二维数组用于分时间段渲染

//判断时间
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用以分时间渲染了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值