废话不多说直接上代码:

效果图:

index.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hour: '',
    min: '',
    second: '',
    m_second: '',
    endDate2:'2018-11-30 18:00:00'
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.countTime()
  },
  countTime() {
    var that = this;
    var date = new Date();
    var now = date.getTime();
    var endDate = new Date(that.data.endDate2);//设置截止时间
    var end = endDate.getTime();
    var leftTime = end - now; //时间差                              
    var d, h, m, s, ms;
    if (leftTime >= 0) {
      d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
      h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
      m = Math.floor(leftTime / 1000 / 60 % 60);
      s = Math.floor(leftTime / 1000 % 60);
      ms = Math.floor(leftTime % 1000);
      ms = ms < 100 ? "0" + ms : ms
      s = s < 10 ? "0" + s : s
      m = m < 10 ? "0" + m : m
      h = h < 10 ? "0" + h : h
    } else {
      console.log('已截止')
    }
    that.setData({
      hour: h,
      min: m,
      second: s,
      m_second: ms
    })
    //递归每秒调用countTime方法,显示动态时间效果
    setTimeout(that.countTime, 50);
  },

  
})

index.wxml

<view>
<text>{{hour+':'+min+":"+second+":"+m_second}}</text>
</view>

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注