工具方法代码如下:
/**
* 获取时间差
* @param timeStart
* @param timeEnd
*/
function getCostTimeString(timeStart, timeEnd,level) {
var timeCost = timeEnd.getTime() - timeStart.getTime(); //时间差的毫秒数
var days = Math.floor(timeCost / (24 * 3600 * 1000)); // 计算出相差天数
//计算出小时数
var leave1 = timeCost % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
var hours = Math.floor(leave1 / (3600 * 1000));
//计算出相差分钟数
var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
var minutes = Math.floor(leave2 / (60 * 1000));
//计算出相差秒数
var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
var seconds = Math.round(leave3 / 1000);
if (level === 1) {
return seconds + "秒";
}
if (level === 2) {
return minutes + "分钟" + seconds + "秒";
}
if (level === 3) {
return hours+"小时"+minutes + "分钟" + seconds + "秒";
}
if (level === 4) {
return days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒";
}
return minutes + "分钟" + seconds + "秒";
}