下面我就為大家分享一篇JS計算距當前時間的時間差實例,具有很好的參考價值,希望對大家有所幫助。
如下:
/** * JS获取距当前时间差 * * @param int time 时间戳格式 * */ function get_time_diff(time) { var diff = ''; var time_diff = new Date().getTime() - time; //时间差的毫秒数 //计算出相差天数 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; }
上面是我整理給大家的,希望今後會對大家有幫助。
相關文章:
Laravel如何在Laravel中整合Bootstrap 4?
以上是使用JS如何獲得當前時間差的詳細內容。更多資訊請關注PHP中文網其他相關文章!