newDate(stringTime) {
// var stringTime = "2021-07-10 10:21:12";
let timestamp2 = stringTime.replace(/-/g, '/') //为了兼容IOS,需先将字符串转换为'2021/07/10 10:21:12'
//这一点很重要,一定要在手机端测试
timestamp2 = Date.parse(timestamp2) //返回'2021-07-10 10:21:12'的时间戳
let endTimes = timestamp2 + (360 * 60 * 60 * 1000)
// console.log(stringTime + "的时间戳为:" + timestamp2);
// console.log('48小时之后的时间戳为:' + endTime)
this.daojishi(endTimes)
// return timestamp2;
},
daojishi(endTimes) {
let endTime = endTimes;
let nowTime = new Date().getTime();
let Cha = endTime - nowTime;
let days = parseInt(Cha / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
let hours = parseInt(Cha / 1000 / 60 / 60 % 24, 10); //计算剩余的小时
let minutes = parseInt(Cha / 1000 / 60 % 60, 10); //计算剩余的分钟
let seconds = parseInt(Cha / 1000 % 60, 10); //计算剩余的秒数
days = this.checkTime(days);
hours = this.checkTime(hours);
minutes = this.checkTime(minutes);
seconds = this.checkTime(seconds);
let that = this;//小程序的生命周期和Vue不太一样,所以自调用之前要把this重新定义一下
setTimeout(function () {
that.daojishi(endTimes);
}, 1000);
let useTime = days + "天" + hours + "小时 " + minutes + "分" + seconds + "秒"
this.setData({
useTime: useTime //小程序的赋值方式,假如是普通的Vue语法是不需要这样赋值的
})
},
checkTime(i) { //将0-9的数字前面加上0,例1变为01
if (i < 10) {
i = "0" + i;
}
return i;
},
网友评论