年月日时分秒计算时间差
作者:互联网
// 计算时间区间, 如有返回开始时间和结束时间,则计算区间,
// 如结束时间为空,则结束时间为当前时间,来算取已用时
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;
},
标签:startDate,endDate,计算,时间差,returnStr,var,年月日,时分秒,1000 来源: https://blog.csdn.net/weixin_45895806/article/details/119085007