/** * JS獲取距當前時間差 * * @param int time 時間戳格式 * */ function get_time_diff(time) { var diff = ''; var time_diff = new Date().getTime() - time; //時間差的毫秒數 /... ...
/** * 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; }