如下所示:
/** * JS獲取距當(dāng)前時(shí)間差 * * @param int time 時(shí)間戳格式 * */ function get_time_diff(time) { var diff = ''; var time_diff = new Date().getTime() - time; //時(shí)間差的毫秒數(shù) //計(jì)算出相差天數(shù) var days = Math.floor(time_diff / (24 * 3600 * 1000)); if (days > 0) { diff += days + '天'; } //計(jì)算出小時(shí)數(shù) var leave1 = time_diff % ( 24 * 3600 * 1000); var hours = Math.floor(leave1 / (3600 * 1000)); if (hours > 0) { diff += hours + '小時(shí)'; } else { if (diff !== '') { diff += hours + '小時(shí)'; } } //計(jì)算相差分鐘數(shù) var leave2 =leave1 % (3600 * 1000); var minutes = Math.floor(leave2 / (60 * 1000)); if (minutes > 0) { diff += minutes + '分'; } else { if (diff !== '') { diff += minutes + '分'; } } //計(jì)算相差秒數(shù) var leave3 = leave2%(60*1000); var seconds = Math.round(leave3/1000); if (seconds > 0) { diff += seconds + '秒'; } else { if (diff !== '') { diff += seconds + '秒'; } } return diff; }
以上這篇JS計(jì)算距當(dāng)前時(shí)間的時(shí)間差實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com