我想要基于给定日期,开始时间和结束时间的时隙。
输入是这样的:
这是统计时间和结束时间列表
[
{
"StartTime" : "10:00",
"endTime" : "14:00"
},
{
"StartTime" : "16:00",
"endTime" : "18:00"
}
]
其他数据:
"SlotDuration": 60 //in minutes
"minStartTimeDufference": 15 // in minutes
然后输出就像
[
"10:00-11:00",
"10:15-11:15",
"10:30-11:30",
"10:45-11:45",
"11:00-12:00",
"11:15-12:15",
"11:30-12:30",
"11:45-12:45",
"12:00-13:00",
"12:15-13:15",
"12:30-13:30",
"12:45-13:45",
"13:00-14:00",
"16:00-17:00",
"16:15-17:15",
"16:30-17:30",
"16:45-17:45",
"17:00-18:00"
]
minSlotDuration 是从开始时间到结束时间的时间段持续时间,而 minStartTimeDufference 是考虑制作时间段的时间,我们也可以说它的持续时间。通过查看输出了解更多想法。
我尝试过的事情:
let parseTime = (s) => {
let c = s.split(':');
return parseInt(c[0]) * 60 + parseInt(c[1]);
}
let convertHours = (mins) => {
let hour = Math.floor(mins / 60);
mins = mins % 60;
let converted = pad(hour, 2) + ':' + pad(mins, 2);
return converted;
}
let pad = (str, max) => {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
let calculate_time_slot = (start_time, end_time, interval) => {
let i, formatted_time;
let time_slots = new Array();
for (let i = start_time; i <= end_time; i = i + interval) {
formatted_time = convertHours(i);
time_slots.push(formatted_time);
}
return time_slots;
}
let date = "20-07-2019"
let startTime = "10:00";
let endTime = "14:00";
let interval = 60; // in minutes
start_time = parseTime(startTime)
end_time = parseTime(endTime)
let times_ara = calculate_time_slot(start_time, end_time, interval);
console.log(times_ara);
答案 0 :(得分:2)
可以通过从开始分钟到结束分钟减去SlotDuration的循环进行简化:
var SlotDuration = 60, minStartTimeDufference = 15, result = []
var data = [ { StartTime: "10:00", endTime: "14:00" },
{ StartTime: "16:00", endTime: "18:00" } ]
const getMinutes = s => s.slice(0, 2) * 60 + +s.slice(-2)
const getTime = m => new Date(m * 6e4).toJSON().slice(11, 16)
for (var item of data) {
var start = getMinutes(item.StartTime), end = getMinutes(item.endTime);
for (var m = start; m <= end - SlotDuration; m += minStartTimeDufference)
result.push(getTime(m) + '-' + getTime(m + SlotDuration))
}
console.log( result )
答案 1 :(得分:1)
使用Date.UTC()
Date.UTC(年[,月[,日[,小时[,分钟[,秒[, 毫秒]]]]]]])
const range = (start, end, dur = 60, diff = 15) => {
start = parse(start)
end = parse(end)
let sd = new Date(Date.UTC(2000, 0, 1, start[0], start[1]))
let ed = Date.parse(new Date(Date.UTC(2000, 0, 1, end[0], end[1])))
let td = new Date(sd)
while (Date.parse(td) < ed) {
td = new Date(sd)
td.setUTCMinutes(td.getUTCMinutes() + dur)
console.log(
format(sd.getUTCHours(), sd.getUTCMinutes()) + '-' +
format(td.getUTCHours(), td.getUTCMinutes())
)
sd.setUTCMinutes(sd.getUTCMinutes() + diff)
}
}
const format = (hh, mm) => {
return hh.toString().padStart(2, '0') + ':' + mm.toString().padStart(2, '0')
}
const parse = (time) => {
return time.split(':').map(Number)
}
range("10:00", "18:00", 60, 15)
.as-console-wrapper {
max-height: 100%!important;
}
答案 2 :(得分:0)
在不考虑开始时间之间的最小差异的情况下更新了代码。
let calculate_time_slot = (start_time, end_time, interval) => {
let i, formatted_time;
let time_slots = new Array();
for (let i = start_time; i + interval <= end_time; i = i + interval) {
formatted_time = convertHours(i) + "-" + convertHours(i + interval);
time_slots.push(formatted_time);
}
return time_slots;
}
如果要在开始时间之间增加最小差异,请将i = i + interval
更改为i = i + minStartDifference
答案 3 :(得分:0)
这是我的解决方法:
演示:
var input = [
{
"StartTime" : "10:00",
"endTime" : "14:00"
},
{
"StartTime" : "16:00",
"endTime" : "18:00"
}
];
var slotDuration = 60; //in minutes
var minStartTimeDufference = 15; // in minutes
class Time {
constructor(time) {
if (time) {
this.hour = parseInt(time.split(':')[0]);
this.minute = parseInt(time.split(':')[1]);
}
}
addMinute(minute) {
this.minute += minute;
if (this.minute >= 60) {
this.minute -= 60;
this.hour += 1;
}
}
isBefore(time) {
if (this.hour < time.hour) return true;
else if (this.hour == time.hour && this.minute <= time.minute) return true;
return false;
}
toText() {
let result = '';
if (this.hour < 10) result += `0${this.hour}:`;
else result += `${this.hour}:`;
if (this.minute < 10) result += `0${this.minute}`;
else result += `${this.minute}`;
return result;
}
}
var result = [];
input.forEach((val) => {
let start = new Time(val.StartTime);
let end = new Time(val.endTime);
let temp = new Time(val.StartTime);
temp.addMinute(slotDuration);
while (temp.isBefore(end)) {
result.push(`${start.toText()}-${temp.toText()}`);
start.addMinute(minStartTimeDufference);
temp.addMinute(minStartTimeDufference);
}
})
console.log(result);