vue 或者uniapp 到期时间格式(天-时-分-秒)通用代码

 data() {
      toLiveBtn: "已到期",
      _interval: ""
    };
  },
   //把时间日期转成时间戳  ios不支持2019-1-29 17:13:04 这种"-"的格式 所以就提前转换格式然后再进行秒数转换
      let nowtime = res.time.replace(/-/g, "/");     //转换格式
      let oldtime = res.expire_time.replace(/-/g, "/");   //转换格式

      let time = new Date(nowtime).getTime(); //当前时间

      let endtime = new Date(oldtime).getTime(); //到期时间

      let surplus = (endtime - time) / 1000; //剩余时间
      const that = this;
      that._interval = setInterval(() => {
        if (surplus == 0) {
          // 计时结束,清除缓存
          clearInterval(that._interval);
        } else {
          surplus--;
          let day = parseInt(surplus / 60 / 60 / 24);
          let hr = parseInt((surplus / 60 / 60) % 24);
          let min = parseInt((surplus / 60) % 60);
          let sec = parseInt(surplus % 60);

          day = day > 9 ? day : "0" + day;
          hr = hr > 9 ? hr : "0" + hr;
          min = min > 9 ? min : "0" + min;
          sec = sec > 9 ? sec : "0" + sec;
          that.toLiveBtn = `${day}天${hr}时${min}分${sec}秒`;
        }
      }, 1000);
  //当离开页面时,清除倒计时
  beforeDestroy() {
    clearInterval(this._interval);
  }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值