// 倒计时(传入截止时间的方式)
leftTimer(enddate) {
let leftTime = (new Date(enddate)) - new Date(); //计算剩余的毫秒数
let days = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
let hours = parseInt(leftTime / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
let minutes = parseInt(leftTime / 1000 / 60 % 60, 10); //计算剩余的分钟
let seconds = parseInt(leftTime / 1000 % 60, 10); //计算剩余的秒数
days = this.checkTime(days);
hours = this.checkTime(hours);
minutes = this.checkTime(minutes);
seconds = this.checkTime(seconds);
if (days <= '00' && hours <= '00' && minutes <= '00' && seconds <= '00') {
return null;
}
if (days >= '00' || hours >= '00' || minutes >= '00' || seconds >= '00') {
return minutes + ":" + seconds
}
}
调用
goDate(v){ //传入时间戳
let that = this;
let my_v = this.$utils.formatDateTime(v) //这是把时间戳转换为日期时间格式
let date1 = new Date();
let date2 = new Date(my_v);
if (date2 < date1) {//设置的时间小于现在时间退出
this.times = '00:00';
return;
}else{
this.ordertimer = setInterval(function () {
if (that.$utils.leftTimer(date2) == null) {
clearInterval(that.ordertimer);
that.times = '00:00';
that.ordertimer = null;
return;
}
that.times = that.$utils.leftTimer(date2)
}, 1000);
}
}
网友评论