微信小程序 30分钟倒计时功能

ps:凑个数
getTimeDiff(date) {
    let _this = this;
    let curTime = new Date(date)
    _this.countDown(_this.timeFormatConvert(new Date(curTime.setMinutes(curTime.getMinutes() + 30))))
  },
  timeFormatConvert(e) {
    const Y = e.getFullYear(); // 年
    const M = this.prefixZero(e.getMonth() + 1); // 月
    const D = this.prefixZero(e.getDate()); // 日
    const H = this.prefixZero(e.getHours()); // 时
    const Mi = this.prefixZero(e.getMinutes()); // 分
    const S = this.prefixZero(e.getSeconds()); // 秒
    return Y + "-" + M + "-" + D + " " + H + ":" + Mi + ":" + S;
  },
  prefixZero(num = 0, n = 2) {
    return (Array(n).join("0") + num).slice(-n);
  },
  /**
   * 倒计时
   * @param endTime 结束日期+时间
   * **/
  countDown(endTime) {
    var that = this;
    that.setData({
      timer: setInterval(function () {
        var downTime = parseInt(new Date(endTime.replace(/-/g, "/")).getTime() - new Date().getTime())
        // 倒计时结束
        if (downTime <= 0) {
          that.setData({
            timedjs: "00:00"
          })
          //结束周期计时器
          clearInterval(that.data.timer);
          return;
        }
        //计算距离活动还有多少天、时、分、秒
        var d = parseInt(downTime / 1000 / 3600 / 24);
        var h = parseInt(downTime / 1000 / 3600 % 24);
        var m = parseInt(downTime / 1000 / 60 % 60);
        var s = parseInt(downTime / 1000 % 60);
        //统一格式的显示
        d < 10 ? d = '0' + d : d;
        h < 10 ? h = '0' + h : h;
        m < 10 ? m = '0' + m : m;
        s < 10 ? s = '0' + s : s;
        //同步显示
        that.setData({
          timedjs: m + ":" + s
        })
      }, 1000)
    })
  },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新时代_打工人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值