从一系列繁忙的时间范围中获取可用的时间范围 [英] Get available time ranges from an array of busy time ranges
问题描述
假设您有一个会议的 BUSY 时间范围数组
Lets say you have an array of BUSY time ranges for a meeting
[{'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 次,与上述时间相反.喜欢...
I would like to get in return an array of AVAILABLE times in a 24 hour frame, that are the opposite of the above times. Like...
[{'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()
方法.
I have tried using moment.js as well as https://www.npmjs.com/package/moment-range, specifically the .subtract()
method.
我知道类似的stackoverflow问题,但是无法找到适用于此格式的问题(使用javascript),momentJS和优雅的ES6数组方法解决方案.
I am aware of similar stackoverflow questions but couldn't find ones that were applicable to this format, in javascript, with momentJS, and elegant ES6 array method solution.
推荐答案
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>
我已使用utc时间戳比较时间,并假设所有间隔都属于一天.某些情况可能会丢失,但是您可以采用这个想法.我已经利用了贪婪算法.首先根据开始时间对所有间隔进行排序.然后遍历排序后的数组以选择正确的时间间隔
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
这篇关于从一系列繁忙的时间范围中获取可用的时间范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!