设置倒计时
此方法仅限不超过一个月的倒计时文章来源地址https://www.toymoban.com/news/detail-505904.html
<view>
<text>{{year>9?year:"0"+year}}</text>年
<text>{{month>9?month:"0"+month}}</text>月
<text>{{day>9?day:"0"+day}}</text>日
<text>{{hour>9?hour:"0"+hour}}</text>时
<text>{{minute>9?minute:"0"+minute}}</text>分
<text>{{second>9?second:"0"+second}}</text>秒
</view>
page({
data: {
day:0,
hour:0,
minute:0,
second:0,
},
// 设置倒计时 计算距离结束还有几天几时几分几秒
setCountDown(endTime) {
var s1 = new Date(endTime.replace(/-/g, "/")),
s2 = new Date(),
runTime = parseInt((s1.getTime() - s2.getTime()) / 1000);
var year = Math.floor(runTime / 86400 / 365);
runTime = runTime % (86400 * 365);
var month = Math.floor(runTime / 86400 / 30);
runTime = runTime % (86400 * 30);
var day = Math.floor(runTime / 86400);
runTime = runTime % 86400;
var hour = Math.floor(runTime / 3600);
runTime = runTime % 3600;
var minute = Math.floor(runTime / 60);
runTime = runTime % 60;
var second = runTime;
if (day < 0 || hour < 0 || minute < 0 || second < 0) {
console.log("已经结束")
return;
}
this.setData({
day,
hour,
minute,
second
})
console.log("日" + day + ",时" + hour + ",分" + minute + ",秒" + second)
let that = this
let _setItv = setInterval(() => {
that.countDownStart()
}, 1000);
this.setData({
setItv: _setItv
})
},
// 开始做倒计时
countDownStart() {
if (this.data.day == 0 && this.data.hour == 0 && this.data.minute == 0) {
if (this.data.second > 0) {
this.setData({
second: this.data.second - 1
})
} else if (this.data.second == 0) {
clearInterval(this.data.setItv)
console.log("倒计时结束")
}
} else if (this.data.day == 0 && this.data.hour == 0) {
// 不会出现minute等于0
if (this.data.minute > 0) {
if (this.data.second > 0) {
this.setData({
second: this.data.second - 1
})
} else if (this.data.second == 0) {
this.setData({
second: 59,
minute: this.data.minute - 1
})
}
}
} else if (this.data.day == 0) {
if (this.data.hour > 0) {
if (this.data.minute > 0) {
if (this.data.second > 0) {
this.setData({
second: this.data.second - 1
})
} else {
this.setData({
second: 59,
minute: this.data.minute - 1
})
}
} else {
this.setData({
second: 59,
minute: 59,
hour: this.data.hour - 1
})
}
}
} else {
if (this.data.day > 0) {
if (this.data.hour > 0) {
if (this.data.minute > 0) {
if (this.data.second > 0) {
this.setData({
second: this.data.second - 1
})
} else {
this.setData({
second: 59,
minute: this.data.minute - 1
})
}
} else {
this.setData({
second: 59,
minute: 59,
hour: this.data.hour - 1
})
}
} else {
this.setData({
second: 59,
minute: 59,
hour: 23,
day: this.data.day - 1
})
}
}
}
},
})
文章来源:https://www.toymoban.com/news/detail-505904.html
到了这里,关于微信小程序实现给一个时间去设置倒计时的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!