let startTime = ’2020-01-01 10:00:00‘;
let endTime = ’2020-01-02 11:00:00‘;
twoTimeInterval(startTime, endTime) {
// 开始时间
let d1 = startTime.replace(/\-/g, "/");
let date1 = new Date(d1);
// 结束时间
let d2 = endTime.replace(/\-/g, "/");
let date2 = new Date(d2);
// 时间相差秒数
let dateDiff = date2.getTime() - date1.getTime();
// 计算出相差天数
let days = Math.floor(dateDiff / (24 * 3600 * 1000));
// 计算出小时数
let residue1 = dateDiff % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
let hours = Math.floor(residue1 / (3600 * 1000));
// 计算相差分钟数
let residue2 = residue1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
let minutes = Math.floor(residue2 / (60 * 1000));
// 计算相差秒数
let residue3 = residue2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
let seconds = Math.round(residue3 / 1000);
return ( ( days === 0 ) ? "" : days + "d" ) +
( ( hours === 0 ) ? "" : hours + "h" ) +
( ( minutes === 0 ) ? "" : minutes + "m" ) +
( ( seconds === 0 ) ? "" : seconds + "s" );
}
网友评论