问题
如题所示
答案
相关源码:
export function getTimeInterval(startDate, endDate = Date.now()) {
if (arguments.length === 0) {
return null
}
let startTime;
let endTime;
if (typeof startDate === 'object') {
startTime = startDate.getTime();
} else {
startTime = startDate;
}
if (typeof endDate === 'object') {
endTime = endDate.getTime();
} else {
endTime = endDate;
}
let dateInterval = endTime - startTime;
// //计算出相差天数
let days = Math.floor(dateInterval / (24 * 60 * 60 * 1000));
//计算小时数
let hourLevel = dateInterval % (24 * 60 * 60 * 1000);
let hours = Math.floor(hourLevel / (60 * 60 * 1000))
//计算分钟数
let minutesLevel = hourLevel % (60 * 60 * 1000);
let minutes = Math.floor(minutesLevel / (60 * 1000));
//计算秒数
let seconds = Math.round((minutesLevel % (60 * 1000)) / 1000);
return `${days} 天 ${hours} 时 ${minutes} 分 ${seconds} 秒`;
}
综上所述,Math.round()
这个方法的作用是四舍五入。
原创文章,作者:dweifng,如若转载,请注明出处:https://blog.ytso.com/272710.html