最近项目上有个需求,计算请假时间,时间有效范围是09:00:00到17:00:00;最好是去掉周末,很遗憾我这里没有排除掉,新人一枚,把自己做的发出来供大家指正一下,如果有更好的办法欢迎交流讨论;
countTime:function(){
const that = this
const startDate = '2020/07/06 17:00:00'
const endDate = '2020/07/10 09:00:00'
const date1 = startDate.substring(0,10)
const date2 = endDate.substring(0,10)
let hours=0
if(date1==date2){//日期相等
let time1
if(that.compareDate(startDate,date1+' 09:00:00') && !that.compareDate(startDate,date1+' 17:00:00')){//比9点大且比5点小
time1 = startDate
}else if(!that.compareDate(startDate,date1+' 09:00:00')){//比9点小
time1 = date1+' 09:00:00'
}else{
time1=0
}
let time2
if(!that.compareDate(date2+' 17:00:00',endDate)){//比5点大
time2 = date2+' 17:00:00'
}else if(that.compareDate(date2+' 17:00:00',endDate) && !that.compareDate(date2+' 09:00:00',endDate)){//比5点小
time2 = endDate
}else{
time2 = 0
}
if(time1==0||time2==0){
hours = 0
return
}
const start_date = new Date(time1)
const end_date = new Date(time2)
let time = (end_date.getTime() - start_date.getTime())/(1000*60*60)
console.log(time)
hours = time.toFixed(2)
}else{
var sArr = date1.split("/");
var eArr = date2.split("/");
var sRDate = new Date(sArr[0], sArr[1], sArr[2]);
var eRDate = new Date(eArr[0], eArr[1], eArr[2]);
var result = (eRDate-sRDate)/(24*60*60*1000);
if(that.compareDate(date1+' 09:00:00',startDate)){//开始<=9
if(that.compareDate(date2+' 09:00:00',endDate)){//结束<=9
hours = (result-1)*8+8
}else if(!that.compareDate(date2+' 09:00:00',endDate) && !that.compareDate(endDate,date2+' 17:00:00')){//结束>9 && <17
const start_date = new Date(date2+' 09:00:00')
const end_date = new Date(endDate)
let time = (end_date.getTime() - start_date.getTime())/(1000*60*60)
hours = (result-1)*8+time+8
}else if(that.compareDate(endDate,date2+' 17:00:00')){//结束>=17
hours = (result-1)*8+8+8
}
}else if(!that.compareDate(date1+' 09:00:00',startDate) && !that.compareDate(startDate,date1+' 17:00:00')){//开始>9 && 开始<17
if(that.compareDate(date2+' 09:00:00',endDate)){//结束<=9
const start_date = new Date(startDate)
const end_date = new Date(date1+' 17:00:00')
let time = (end_date.getTime() - start_date.getTime())/(1000*60*60)
hours = (result-1)*8+time
}else if(!that.compareDate(date2+' 09:00:00',endDate) && !that.compareDate(endDate,date2+' 17:00:00')){//结束>9 && <17
const start_date1 = new Date(startDate)
const end_date1 = new Date(date1+' 17:00:00')
let time1 = (end_date1.getTime() - start_date1.getTime())/(1000*60*60)
const start_date2 = new Date(date2+' 09:00:00')
const end_date2 = new Date(endDate)
let time2 = (end_date2.getTime() - start_date2.getTime())/(1000*60*60)
hours = (result-1)*8+time1+time2
}else if(endDate,date2+' 17:00:00'){//结束>=17
const start_date1 = new Date(startDate)
const end_date1 = new Date(date1+' 17:00:00')
let time1 = (end_date1.getTime() - start_date1.getTime())/(1000*60*60)
hours = (result-1)*8+time1+8
}
}else if(that.compareDate(startDate,date1+' 17:00:00')){//开始>=17
if(that.compareDate(date2+' 09:00:00',endDate)){//结束<=9
hours = (result-1)*8
}else if(!that.compareDate(date2+' 09:00:00',endDate) && !that.compareDate(endDate,date2+' 17:00:00')){//结束>9 && <17
const start_date = new Date(date2+' 09:00:00')
const end_date = new Date(endDate)
let time = (end_date.getTime() - start_date.getTime())/(1000*60*60)
hours = (result-1)*8+time
}else if(that.compareDate(endDate,date2+' 17:00:00')){//结束>=17
hours = (result-1)*8+8
}
}
}
return hours
},
//日期大小比较
compareDate(date1,date2){
var oDate1 = new Date(date1);
var oDate2 = new Date(date2);
if(oDate1.getTime() >= oDate2.getTime()){
return true; //第一个大=
} else {
return false; //第二个大
}
},
遗留问题就是周末的时间的排除,这里找了网上一下大佬的办法:
loadWeek:function(){
const startDate = '2020/07/06 17:00:00'
var dt=new Date(startDate);
var weekDay = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
console.log("打印数值---",dt.getDay());
console.log("今天是:",weekDay[dt.getDay()]);
},
待完善