timeFormat:function(n) {
return n >= 0 && n < 10 ? '0' + n : '' + n;
},
getDate: function () {
var obj = null;
var countDownArr = [];
var oDate = new Date(); //获取现在日期对象
var oldTime = oDate.getTime(); //现在距离1970年的毫秒数
var newDate = new Date('2019/1/30 00:00:00'); //获取指定日期对象
var newTime = newDate.getTime(); //2018年距离1970年的毫秒数
var second = Math.floor((newTime - oldTime) / 1000); //未来时间距离现在的秒数
var day = Math.floor(second / 86400); //整数部分代表的是天;一天有24*60*60=86400秒 ;
second = second % 86400; //余数代表剩下的秒数;
var hour = Math.floor(second / 3600); //整数部分代表小时;
second %= 3600; //余数代表 剩下的秒数;
var minute = Math.floor(second / 60);
second %= 60;
obj = {
day: this.timeFormat(day),
hou: this.timeFormat(hour),
min: this.timeFormat(minute),
sec: this.timeFormat(second)
}
countDownArr.push(obj);
this.setData({
countDownList: countDownArr
})
setInterval(this.getDate, 1000); //
},
onLoad: function (options) {
this.getDate();
}
<view wx:for="{{countDownList}}" wx:key="countDownList">
<text>{{item.day}}</text>天
<text>{{item.hou}}</text>时
<text>{{item.min}}</text>分
<text>{{item.sec}}</text>秒
</view>
复制代码
转载于:https://juejin.im/post/5c17c4636fb9a049b13e1e6e