js根据起始和截止时间和间隔时间 计算预约时间段

2022-07-07  本文已影响0人  爱学习的小仙女早睡早起

js根据起始和截止时间和间隔时间 计算预约时间段

image
// 查询预约配置
    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分钟
        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()}:${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()}:${date.getMinutes()==0?'00':date.getMinutes()}`
            attrTime.push(`${time1}~${time2}`)
            time1=time2
            n++
        }

        return attrTime
    },

image
上一篇下一篇

猜你喜欢

热点阅读