gpt4 book ai didi

javascript - 从繁忙时间范围数组中获取可用时间范围

转载 作者:行者123 更新时间:2023-11-30 07:12:37 25 4
gpt4 key购买 nike

假设您有一组BUSY session 时间范围

[{'start':'9:00 AM', 'end':'10:00 AM'},
{'start':'12:00 PM', 'end':'2:00 PM'},
{'start':'5:00 AM', 'end':'7:00 PM'}]

我想在 24 小时内返回一组 AVAILABLE 时间,这与上述时间相反。喜欢...

[{'start':'00:00 AM', 'end':'9:00 AM'},
{'start':'10:00 AM', 'end':'12:00 PM'},
{'start':'2:00 PM', 'end':'5:00 PM'},
{'start':'7:00 PM', 'end':'11:59 PM'}]

我试过使用 moment.js 和 https://www.npmjs.com/package/moment-range ,特别是 .subtract() 方法。

我知道类似的 stackoverflow 问题,但找不到适用于这种格式的问题,在 javascript 中,使用 momentJS 和优雅的 ES6 数组方法解决方案。

最佳答案

function giveUtc(start) {
var t = moment().format("YYYY-MM-DD")
var t1 = t + " " + start
return moment(t1, "YYYY-MM-DD h:mm A").format()

}


const timeRange = [{
'start': '9:00 AM',
'end': '10:00 AM'
},
{
'start': '12:00 PM',
'end': '2:00 PM'
},
{
'start': '5:00 PM',
'end': '7:00 PM'
},
{
"start": "11:00 AM",
"end": "3:00 PM",
},
{
"start": "6:00 PM",
"end": "9:00 PM",
}]


timeRange.sort((a, b) => {
var utcA = giveUtc(a.start)
var utcB = giveUtc(b.start)
if (utcA < utcB) {
return -1

}
if (utcA > utcB) {
return 1


}
return 0
})
const availableTimeArray = []

let endTimeFarthest = moment(giveUtc("0.00 AM"))
let startTimeMinimum = moment(giveUtc("12.59 PM"))
timeRange.forEach((element, index) => {
let currentEndTime = moment(giveUtc(element.end))
const currentStartTime = moment(giveUtc(element.start))
if (currentStartTime.isBefore(startTimeMinimum)) {
startTimeMinimum = currentStartTime
}
if (currentEndTime.isAfter(endTimeFarthest)) {
endTimeFarthest = currentEndTime
}
/* console.log(startTimeMinimum.format("h:mm A"), endTimeFarthest.format("h:mm A")) */
if (index === timeRange.length - 1) {
if (timeRange.length === 1) {
availableTimeArray.push({
start: "00:00 AM",
end: currentStartTime.format("h:mm A")
})
}
availableTimeArray.push({
//start: currentEndTime.format("h:mm A"),
start: endTimeFarthest.format("h:mm A"),
end: "11.59 PM"
})

} else {
const nextBusyTime = timeRange[index + 1]
const nextStartTime = moment(giveUtc(nextBusyTime.start))
if (index === 0) {
availableTimeArray.push({
start: "00:00 AM",
end: currentStartTime.format("h:mm A")
})
}
let endTimeToCompare = currentEndTime.isBefore(endTimeFarthest) ?
endTimeFarthest :
currentEndTime
if (endTimeToCompare.isBefore(nextStartTime)) {
availableTimeArray.push({
start: endTimeToCompare.format("h:mm A"),
end: nextStartTime.format("h:mm A")
})
}

}

})
console.log(availableTimeArray)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>

I have used utc timestamp to compare between timings and assumed that all the interval belongs to a single day. Some edge case might be missing but you can take the idea. I have made use of greedy algorithm. First sorting all the interval based on the start time. Then iterating through the sorted array to pick correct interval

关于javascript - 从繁忙时间范围数组中获取可用时间范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48407713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com