1. 程式人生 > >js 兩個時間戳之間怎麼計算相隔多少天多少小時多少分多少秒

js 兩個時間戳之間怎麼計算相隔多少天多少小時多少分多少秒

/** * 計算時間戳之間的差值 * * @param start_time 開始時間戳 * @param end_time 結束時間戳 */
function calculateDiffTime(start_time, end_time){ var startTime = 0, endTime = 0 if (start_time < end_time){ startTime = start_time endTime = end_time }else{ startTime = end_time endTime = start_time }
// //計算天數
// var timeDiff = endTime - startTime // var year = Math.floor(timeDiff / 86400 / 365); // timeDiff = timeDiff % (86400 * 365); // var month = Math.floor(timeDiff / 86400 / 30); // timeDiff = timeDiff % (86400 * 30); // var day = Math.floor(timeDiff / 86400); // timeDiff = timeDiff % 86400; // var hour = Math.floor(timeDiff / 3600);
// timeDiff = timeDiff % 3600; // var minute = Math.floor(timeDiff / 60); // timeDiff = timeDiff % 60; // var second = timeDiff; // return [year, month, day, hour, minute, second]
var timeDiff = endTime - startTime var hour = Math.floor(timeDiff / 3600); timeDiff = timeDiff % 3600; var minute = Math.floor(timeDiff / 60
); timeDiff = timeDiff % 60; var second = timeDiff;
return [hour, minute, second]
}