// 计算时间区间, 如有返回开始时间和结束时间,则计算区间,
// 如结束时间为空,则结束时间为当前时间,来算取已用时
getTime(startDate,endDate) {
var end;
if (endDate!='') {
end = new Date(endDate.replace(/-/g, "/"));
}else{
end = new Date(this.parseTime(new Date()).replace(/-/g, "/"));
}
var start = new Date(startDate.replace(/-/g, "/"));
return this.diffTime(start, end);
},
diffTime(startDate, endDate) {
var diff = endDate.getTime() - startDate.getTime(); //时间差的毫秒数
//计算出相差天数
var days = Math.floor(diff / (24 * 3600 * 1000));
//计算出小时数
var leave1 = diff % (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);
var returnStr = seconds + "秒";
if (minutes > 0) {
returnStr = minutes + "分钟" + returnStr;
}
if (hours > 0) {
returnStr = hours + "小时" + returnStr;
}
if (days > 0) {
returnStr = days + "天" + returnStr;
}
return returnStr;
},
计算时间差
最新推荐文章于 2023-05-29 20:23:00 发布