vue实现的倒计时在苹果手机上无效,原因是因为后台返回的时间格式是‘2019-1-29 17:13:04’,而苹果手机只能解析这种时间格式‘YYYY/MM/DD HH:mm:ss’,修改后测试成功的代码如下:
npm install moment -save;
this.countdown();
countdown(){
var date = new Date();
var now = date.getTime();
//设置截止时间
let time = moment(this.activityRules.activityStartDate).format('YYYY/MM/DD HH:mm:ss');
var endDate = new Date(time);
var end = endDate.getTime();
//时间差
var leftTime = end - now;
if (leftTime >= 0) {
let hr = Math.floor(leftTime / 1000 / 60 / 60);
let min = Math.floor(leftTime / 1000 / 60 % 60);
let sec = Math.floor(leftTime / 1000 % 60);
this.hr = hr > 9 ? hr : '0' + hr;
this.min = min > 9 ? min : '0' + min;
this.sec = sec > 9 ? sec : '0' + sec;
}
//递归每秒调用countTime方法,显示动态时间效果
setTimeout(this.countdown, 1000);
},