HTML
{{left_time_list[0]}}天
{{left_time_list[1]}}时
{{left_time_list[2]}}分
{{left_time_list[3]}}秒
JS
data:{
left_time: '', //活动剩余时间的秒数
timer: '', //倒计时定时器
left_time_list: [], //剩余秒数转换 天,小时 分 秒
},
show() {
this.howtime()
},
howtime() {
userApi.getTime().then((data: any) => {
let dataTime
let yy = new Date(data).getFullYear()
let mm = new Date(data).getMonth() + 1
let dd = new Date(data).getDate()
let hh = new Date().getHours()
let mf = new Date(data).getMinutes() < 10 ? '0' + new Date(data).getMinutes() :
new Date(data).getMinutes()
let ss = new Date(data).getSeconds() < 10 ? '0' + new Date(data).getSeconds() :
new Date(data).getSeconds()
dataTime = `${yy}/${mm}/${dd} ${hh}:${mf}:${ss}`;
//当前时间为9点到明天17点
if (hh >= 9 && hh < 17) {
clearInterval(this.data.timer)
this.getLeftTime(`${yy}/${mm}/${dd} 17:00:00`)
this.setData({ next: false })
if (mm == 12 && dd == 31) {
this.setData({
mm: 1,
dd: 1,
updown: false,
days: true,
})
} else {
if (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) {
if (dd < 31) {
this.setData({
mm: mm,
dd: dd + 1,
updown: false,
days: true,
})
} else {
this.setData({
mm: mm + 1,
dd: 1,
updown: false,
days: true,
})
}
} else if (mm == 2) {
if (yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0) {
if (dd >= 29) {
this.setData({
mm: mm + 1,
dd: 1,
updown: false,
days: true,
})
} else {
this.setData({
mm: mm,
dd: dd + 1,
updown: false,
days: true,
})
}
} else {
if (dd >= 28) {
this.setData({
mm: mm + 1,
dd: 1,
updown: false,
days: true,
})
} else {
this.setData({
mm: mm,
dd: dd + 1,
updown: false,
days: true,
})
}
}
} else if (mm == 4 || mm == 6 || mm == 9 || mm == 11) {
if (dd < 30) {
this.setData({
mm: mm,
dd: dd + 1,
updown: false,
days: true,
})
} else {
this.setData({
mm: mm + 1,
dd: 1,
updown: false,
days: true,
})
}
}
}
} else {
clearInterval(this.data.timer)
this.setData({ next: true })
//如果明天为12月且大于31日那就新增一年
if (mm == 12 && dd == 31) {
this.getLeftTime(`${yy + 1}/${mm = 1}/${dd = 1} 09:00:00`)
this.setData({
mm: 1,
dd: 2,
updown: true,
days: true,
})
} else {
if (mm == 12 && dd == 30) {
this.getLeftTime(`${yy}/${mm}/${dd +1} 09:00:00`)
this.setData({
mm: 1,
dd: 1,
updown: true,
days: true,
})
} else {
//月份为月末的时候月份加一
if (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) {
if (dd < 30) {
this.getLeftTime(`${yy}/${mm}/${dd + 1} 09:00:00`)
this.setData({
mm: mm,
dd: dd + 2,
updown: true,
days: true,
})
} else {
if (dd == 30) {
this.getLeftTime(`${yy}/${mm}/${dd + 1} 09:00:00`)
this.setData({
mm: mm + 1,
dd: 1,
updown: true,
days: true,
})
} else {
this.getLeftTime(`${yy}/${mm + 1}/${dd = 1} 09:00:00`)
this.setData({
mm: mm + 1,
dd: 2,
updown: true,
days: true,
})
}
}
} else if (mm == 2) {
if (yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0) {
// console.log(yy+'润') 29
if (dd >= 29) {
this.getLeftTime(`${yy}/${mm + 1}/${dd = 1} 09:00:00`)
this.setData({
mm: mm + 1,
dd: 2,
updown: true,
days: true,
})
} else {
if (dd < 28) {
this.getLeftTime(`${yy}/${mm}/${dd + 1} 09:00:00`)
this.setData({
mm: mm,
dd: dd + 2,
updown: true,
days: true,
})
} else {
this.getLeftTime(`${yy}/${mm}/${dd + 1} 09:00:00`)
this.setData({
mm: 3,
dd: 1,
updown: true,
days: true,
})
}
}
} else {
// console.log(yy+'不润') 28
if (dd >= 28) {
this.getLeftTime(`${yy}/${mm + 1}/${dd = 1} 09:00:00`)
this.setData({
mm: mm + 1,
dd: 2,
updown: true,
days: true,
})
} else {
if (dd < 27) {
this.getLeftTime(`${yy}/${mm}/${dd + 1} 09:00:00`)
this.setData({
mm: mm,
dd: dd + 2,
updown: true,
days: true,
})
} else {
this.getLeftTime(`${yy}/${mm}/${dd + 1} 09:00:00`)
this.setData({
mm: 3,
dd: 1,
updown: true,
days: true,
})
}
}
}
} else if (mm == 4 || mm == 6 || mm == 9 || mm == 11) {
if (dd < 29) {
this.getLeftTime(`${yy}/${mm}/${dd + 1} 09:00:00`)
this.setData({
mm: mm,
dd: dd + 2,
updown: true,
days: true,
})
} else {
if (dd == 29) {
this.getLeftTime(`${yy}/${mm}/${dd + 1} 09:00:00`)
this.setData({
mm: mm + 1,
dd: 1,
updown: true,
days: true,
})
} else {
this.getLeftTime(`${yy}/${mm + 1}/${dd = 1} 09:00:00`)
this.setData({
mm: mm + 1,
dd: 2,
updown: true,
days: true,
})
}
}
}
}
}
}
})
},
getLeftTime(end_time: any) {
let left_time = this.getTimestap(end_time);
this.initDate(left_time)
this.data.timer = setInterval(() => {
if (left_time-- === 0) {
this.setData({
left_time: 0,
left_time_list: this.formatSeconds(0)
})
setTimeout(() => {
this.gudian()
}, 1000)
clearInterval(this.data.timer)
// console.log(this.data.timer)
this.howtime()
console.log("执行成功")
} else {
this.initDate(left_time)
}
}, 1000);
},
initDate(e) {
let left_time_list = this.formatSeconds(e)
this.setData({
left_time: e,
left_time_list,
})
},
getTimestap(end_time: any) {
// 当前时间
var currentTime = parseInt(new Date().getTime() / 1000);
// 未来时间
var futureTime = parseInt(new Date(end_time.replace(/-/g, '/')).getTime() / 1000); //ios无法解析
return futureTime <= currentTime ? 0 : futureTime - currentTime;
},
formatSeconds(value: any) {
let time = [],
day = parseInt(value / 86400),
hour = parseInt((value % 86400) / 3600),
min = parseInt(((value % 86400) % 3600) / 60),
sec = parseInt(((value % 86400) % 3600) % 60);
time[0] = day > 0 ? this.addZero(day) : this.addZero(0);
time[1] = hour > 0 ? this.addZero(hour) : this.addZero(0);
time[2] = min > 0 ? this.addZero(min) : this.addZero(0);
time[3] = sec > 0 ? this.addZero(sec) : this.addZero(0);
return time;
},
formatDate(e: any) {
let list = JSON.parse(e);
for (let i = 0; i < list.length; i++) {
list[i] = list[i].toString().split('');
}
return list;
},
addZero(num: any) {
return num < 10 ? "0" + num : num;
},
onUnload() {
if (this.data.timer) {
// console.log('销毁计时器')
clearInterval(this.data.timer)
}
},
![](https://i-blog.csdnimg.cn/blog_migrate/4b4a4aac4713f7fd5f2846448b5a904a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e1385d0c962fc7ed7accb225ac74e955.png)