1:组件需求
二:合并交集时间段。代码
timeSlotConfirm() {
const timeSlotItem = this.timeSlotForm.timeSlotItem.join("-");
const currentSlotItem = this.timeSlotForm.timeSlotItem;
// 周一添加
if (this.addTimeSlotFlag == "one") {
// 已选
if (this.form.one.length === 0) {
this.form.one.push(timeSlotItem);
this.mondayTimeList=this.form.one
} else {
this.timeResult = [];
this.joinTime(
this.form.one,
currentSlotItem
);
this.form.one = this.timeResult
this.mondayTimeList = this.form.one
}
}
}
// 时间段合并
compareTime(t1, t2, t3, t4) {
let d = new Date();
let ft1 = d.setHours(
t1.split(":")[0],
t1.split(":")[1],
t1.split(":")[2]
);
let ft2 = d.setHours(
t2.split(":")[0],
t2.split(":")[1],
t2.split(":")[2]
);
let ft3 = d.setHours(
t3.split(":")[0],
t3.split(":")[1],
t3.split(":")[2]
);
let ft4 = d.setHours(
t4.split(":")[0],
t4.split(":")[1],
t4.split(":")[2]
);
if (ft3 >= ft1 && ft3 <= ft2 && ft4 >= ft2) {
return {
t: t1 + "-" + t4,
flag: false,
};
} else if (ft1 >= ft3 && ft1 <= ft4 && ft2 >= ft4) {
return {
t: t3 + "-" + t2,
flag: false,
};
} else if (ft3 > ft2 || ft1 > ft4) {
return {
t: t3 + "-" + t4,
flag: true,
};
} else if (ft1 >= ft3 && ft2 <= ft4) {
return {
t: t3 + "-" + t4,
flag: false,
};
} else if (ft1 <= ft3 && ft2 >= ft4) {
return {
t: t1 + "-" + t2,
flag: false,
};
}
},
// 合并交集时间段
joinTime(data, currentArr) {
let tempArr = data.slice(0);
while (tempArr.length) {
let item = tempArr.shift();
const itemArr = item.split("-");
const time = this.compareTime(
itemArr[0],
itemArr[1],
currentArr[0],
currentArr[1]
);
if (tempArr.length) {
if (time.flag) {
this.timeResult.push(item);
this.joinTime(tempArr, time.t.split('-'));
return false
} else if (!time.flag) {
this.joinTime(tempArr, time.t.split('-'));
return false
}
} else {
if (time.flag) {
this.timeResult.push(item, time.t);
} else if (!time.flag) {
this.timeResult.push(time.t);
}
}
}
},