Vue使用过滤器对时间戳进行处理

本文介绍了如何在Vue.js应用中创建自定义过滤器,用于将时间戳转换为易读的日期和时间格式。通过实例代码,展示了过滤器的用法,帮助开发者更好地展示和处理时间数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 处理时间
  filters: {
    uptime(val) {
      let oldDate = new Date(val*1000);
      console.log(oldDate.toTimeString().split(' ')[0].slice(0,5));
      let nowDate = new Date();
      let time = nowDate.getTime();
      let difference = (time - val*1000);
      let h24 = 24 * 60 * 60*1000;
      let h1 = 60 * 60 *1000;
      let h48 = 48 * 60 * 60*1000;
      let s1 = 60*1000
      
      if (difference > h48) {
        // return `${oldDate.getMonth()+1}月${oldDate.getDate()}日`;
        return `${oldDate.getMonth()+1}-${oldDate.getDate()} ${oldDate.toTimeString().split(' ')[0].slice(0,5)}`;
      } else if(difference > h24 && difference < h48) {
        return `昨天`;
      }else if(h1<difference && difference<h24){
        // 大于一小时小于24小时
        return `${Math.floor(difference/1000/60/60)}小时前`
      }else if(difference>s1 && difference<h1){
        //大于一分钟小于一小时
        return Math.round((difference / (1000 * 60))) + '分钟前'
      }else if(difference<s1){
        //小于一分钟
        return Math.round((difference / (1000))) + '秒前'
      }
    }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值