1、js根据起始和截止时间和间隔时间 计算预约时间段
// 查询预约配置
getConfig(){
this.morningTimePeriodStart = "2022-07-06 9:00"
this.morningTimePeriodEnd = "2022-07-06 12:00"
this.step = "10"
this.afternoonPeriodStart = "9:00"
this.afternoonPeriodEnd = "12:00"
this.workDays = ["1", "2", "3", "4", "5", "6", "7"]
let arr=this.computeBtnTime(this.morningTimePeriodStart, this.morningTimePeriodEnd, this.step)
console.log(11222333, arr)
},
// 计算可选时段 **
computeBtnTime(startDate, endDate, space){ // space=分钟
// 输入两个时间startDate, endDate,还有时间间隔space(分钟),返回两个时间之间每隔space的时间
// 如:startDate="2022-07-06 9:00",endDate="2022-07-06 12:00",space=30
const startTime = new Date(startDate).getTime() // 将开始时间 转换成时间戳
const endTime = new Date(endDate).getTime()
let n = 1
let step = 0
let date = null
let newStartTime = 0
let attrTime=[]
let time1=`${new Date(startDate).getHours()==0?'00':new Date(startDate).getHours()}:${new Date(startDate).getMinutes()==0?'00':new Date(startDate).getMinutes()}`
let time2=""
while(newStartTime < endTime ){
step = Number(space)* n * 60 *1000 // 将时间间隔转换为时间戳
date = new Date(startTime + step) // 计算新时间
newStartTime = new Date(startTime + step).getTime() // 新时间的时间戳
time2 = `${date.getHours()==0?'00':date.getHours()}:${date.getMinutes()==0?'00':date.getMinutes()}`
attrTime.push(`${time1}~${time2}`)
time1=time2
n++
}
return attrTime
},
2、
// 调用
this.morningArry = this.computeHour(morningTimePeriodStart, morningTimePeriodEnd, intervalPeriod)
// 计算方法
computeHour(time1, time2, interval){
// 输入两个时间time1和time2还有时间间隔interval(分钟),返回time1和time2之间每隔interval的时间
// 时间格式为:HH:mm:ss
// 如:time1=09:00,time2=18:00,interval=30,则返回的时间为:09:00,09:30,10:00,10:30,……,18:00
function getTimeList(time1, time2, interval) { // time1, time2, interval
var timeList = [];
time1 = time1.split(':');
time2 = time2.split(':');
time1 = new Date(0, 0, 0, time1[0], time1[1], 0);
time2 = new Date(0, 0, 0, time2[0], time2[1], 0);
interval = interval * 60 * 1000;
while (time1 < time2) {
timeList.push(time1.format('hh:mm'));
time1 = new Date(time1.getTime() + interval);
}
// timeList.push(time2.format('hh:mm'));
return timeList;
}
Date.prototype.format = function(format) {
var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
}
if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
(this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in date) if (new RegExp("(" + k + ")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length == 1 ? date[k] :
("00" + date[k]).substr(("" + date[k]).length));
return format;
}
return getTimeList(time1, time2, interval)
},