js倒计时效果,传入时间2019-07-30 10:20:00输出xx天xx小时xx分钟xx秒

效果图

在这里插入图片描述

倒计时,主要应用于活动倒计时

当前时间:2019-07-29 10:56:00

//时间传活动截止时间
const app = getApp();
Page({
	data:{
		currentTime:''
	},
	onLoad(){
		app.countdown(this,'2019-07-30 10:20:00');
		console.log(this.data.currentTime);
	},
	onUnload(){
		//关闭页面时记得手动清除定时器
		app.closeinterval();
	}
})

//输出  0天23小时23分钟48秒

app.js

countdown(that, end_time) {
	let app = this;
    let EndTime = new Date(end_time);
    EndTime = EndTime.getTime()
    let NowTime = new Date().getTime();
    let total_micro_second = EndTime - NowTime || [];
    // 渲染倒计时时钟
    that.setData({
      currentTime: this.dateformat(total_micro_second)
    });
    if (total_micro_second <= 0) {
      that.setData({
        currentTime: "已经截止"
      });
      clearInterval(timer)
      return;
    }
    timer = setTimeout(function() {
      total_micro_second -= 1000;
      app.countdown(that, end_time);
    }, 1000)
  },
  //清除定时器
  closeinterval() {
    clearInterval(timer)
  },
  // 时间格式化输出,如11:03 25:19 每1s都会调用一次
  dateformat(micro_second) {
    //总秒数
    var second = Math.floor(micro_second / 1000);
    // 天数
    var day = Math.floor(second / 3600 / 24);
    // 小时
    var hr = Math.floor(second / 3600 % 24);
    //分钟
    var min = Math.floor(second / 60 % 60);
    // 秒
    var sec = Math.floor(second % 60);
    return day + "天" + hr + "小时" + min + "分钟" + sec + "秒";
  },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一个......

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

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

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

打赏作者

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

抵扣说明:

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

余额充值