JS计算距当前时间的时间差实例

前端之家收集整理的这篇文章主要介绍了JS计算距当前时间的时间差实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如下所示:

//计算出相差天数
var days = Math.floor(time_diff / (24 3600 1000));
if (days > 0) {
diff += days + '天';
}
//计算出小时数
var leave1 = time_diff % ( 24 3600 1000);
var hours = Math.floor(leave1 / (3600 1000));
if (hours > 0) {
diff += hours + '小时';
} else {
if (diff !== '') {
diff += hours + '小时';
}
}
//计算相差分钟数
var leave2 =leave1 % (3600
1000);
var minutes = Math.floor(leave2 / (60 1000));
if (minutes > 0) {
diff += minutes + '分';
} else {
if (diff !== '') {
diff += minutes + '分';
}
}
//计算相差秒数
var leave3 = leave2%(60
1000);
var seconds = Math.round(leave3/1000);
if (seconds > 0) {
diff += seconds + '秒';
} else {
if (diff !== '') {
diff += seconds + '秒';
}
}

return diff;
}

以上这篇JS计算距当前时间的时间差实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

原文链接:https://www.f2er.com/js/34428.html

猜你在找的JavaScript相关文章