# 根据周X HH:mm:ss判断时间是否交叉重叠

``````itemInfo:[
{
period: ['03:20:10','15:20:10'],
weekList: ['周一','周三','周四']
},
{
period: ['02:20:15','03:30:30'],
weekList: ['周三','周五']
},
{
period: ['02:20:15','03:30:30'],
weekList: ['周二']
},
{
period: ['16:20:15','20:30:30'],
weekList: ['周一','周五']
}
],
// 周转化数字
getWeekTransform(week){
let newweek = week.map(item=>{
if(item=='周一'){
return item=1
}else if(item=='周二') {
return item=2
}else if(item=='周三') {
return item=3
}else if(item=='周四') {
return item=4
}else if(item=='周五') {
return item=5
}
})
return newweek
},
getHourTime(val){  // 将'12：30：30'格式的时间换算为秒
let hour = val.substring(0,2)
let minute = val.substring(3,5)
let second = val.substring(6,8)
let sum = Number(hour)*3600+Number(minute)*60+Number(second)
return sum
},
getSelfInterval(a,b){  //a为['16:20:15','20:30:30']时间区间，b为['周一','周三','周四']格式换算后的[1,3,4]数组
let selfInterval = b.map(item=>{   //时间段换算成区间
let sum = []
sum.push(Number(item)*86400 + this.getHourTime(a[0]))
sum.push(Number(item)*86400 + this.getHourTime(a[1]))
return sum
})
console.log(selfInterval)
return selfInterval
},
// 判断数字区间是否存在重叠交叉
getNumInterval(a,b){
let max=[]; // 存储两个数字区间start,如[1000,2000]、[3000,4000]存[1000,3000]
let min=[]; // 存储两个数字区间end,如[1000,2000]、[3000,4000]存[2000,4000]
max.push(a[0])
max.push(b[0])
min.push(a[1])
min.push(b[1])
if(Math.max.apply(null, max) <= Math.min.apply(null, min)){
console.log("区间存在重叠交叉！");
}
},
getItemInfo(){
let arr = this.itemInfo.map(item=>{
return this.getSelfInterval(item.period,this.getWeekTransform(item.weekList))
})
let allInterval = arr.flat(1) //所有区间数组
for(let i = 0;i < allInterval.length; i++){
for(let j = 0;j < allInterval.length; j++){
if(i!==j){   //for循环对比区间，去除自身对比
this.getNumInterval(allInterval[i],allInterval[j])
}
}
}
},
``````

原文作者：汪酱～
原文地址: https://blog.csdn.net/qq_44768564/article/details/118559786
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。