js 计算时间差 可刷新运行 刷新

ios 不支持2019-12-12 12:12:12 这样的日期格式,转换成 2019/12/12 12:12:12 才可以使用 Date函数实例化一个时间

代码:

var y,d, h, m, s, ms;
var nowDate = new Date();
var now = nowDate.getTime();
var endDate = new Date('2018/7/16 9:00:00'.replace(/-/g, '/'));//设置截止时间
var end = endDate.getTime();<!-- +5*60*1000 -->
var leftTime = now-end ; //时间差
 y = Math.floor(leftTime / 1000 / 60 / 60 /24/365);
 d = Math.floor(leftTime / 1000 / 60 / 60 / 24%365);
h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
m = Math.floor(leftTime / 1000 / 60 % 60);
s = Math.floor(leftTime / 1000 % 60);
ms = Math.floor(leftTime % 1000);
if(ms < 100) {
    ms = "0" + ms;
}
if(s < 10) {
    s = "0" + s;
}
if(m < 10) {
    m = "0" + m;
}
if(h < 10) {
    h = "0" + h;
}
console.log(y+'年'+'<br>');
console.log(d+'天'+'<br>');
console.log(h+'小时'+'<br>');
console.log(m+'分钟'+'<br>');
console.log(s+'秒'+'<br>');
console.log(ms+'毫秒'+'<br>');

输出: