There is such a scenario: given a time interval, it is necessary to determine which time range this time interval falls within.
For example, the time range is as follows:
[["00:00","01:00"],["01:00","02:00"],["02:00","03:00"],["03:00","04:00"],["04:00","05:00"],["05:00","06:00"],["06:00","07:00"],["07:00","08:00"],["08:00","09:00"],["09:00","10:00"],["10:00","11:00"],["11:00","12:00"],["12:00","13:00"],["13:00","14:00"],["14:00","15:00"],["15:00","16:00"],["16:00","17:00"],["17:00","18:00"],["18:00","19:00"],["19:00","20:00"],["20:00","21:00"],["21:00","22:00"],["22:00","23:00"],["23:00","24:00"]]
Now given a time interval 10:15-16:38 , then you need to return ["10:00","11:00"],["11:00","12:00"],["12: 00","13:00"],["13:00","14:00"],["14:00","15:00"],["15:00","16:00" ],["16:00","17:00"] These time period ranges.
1. Regular time interval determination
Assuming that the given time interval is [st, et] and the range of the time period is [t1, t2], then the judgment of [st, et] within the range of [t1, t2] is based on the following situations:
- t1 >= st && t2 <= et ;
- st >= t1 && st <= t2 ;
- et >= t1 && et <= t2 ;
Let's take a look at the basis for determining that the given time interval is not within the range of the time period , that is, [st, et] is not within the range of [t1, t2]:
- st < t1 && et < t1 ;
- st > t2 && et > t2 ;
Judging from the above judgment basis, the judgment basis for a given time interval not within the time period range is simpler, and by inverting it, the correct time period range can be obtained . The specific judgment code is given below:
function judge(startTime, endTime) {
// 生成24小时时间区间,跨度为1小时
let timeArrays = new Array(24).fill(['', '']).map((item, index) => [(index < 10 ? '0' + index : index) + ':00', ((index + 1) < 10 ? '0' + (index + 1) : (index + 1)) + ':00']);
return timeArrays.filter(item => !((compare(item[0], startTime) && compare(item[0], endTime)) || (compare(startTime, item[1]) && compare(endTime, item[1]))));
}
function compare(startTime, endTime) {
// 将时间转换为分钟,再进行比较
let startTimes = startTime.split(':');
let endTimes = endTime.split(':');
let startTimeVal = startTimes[0] * 60 + Number(startTimes[1]);
let endTimeVal = endTimes[0] * 60 + Number(endTimes[1]);
return startTimeVal >= endTimeVal;
}
Let's verify the time interval of 10:15-16:38
console.log(JSON.stringify(judge('10:15', '16:38')))
The output is as follows:
[["10:00","11:00"],["11:00","12:00"],["12:00","13:00"],["13:00","14:00"],["14:00","15:00"],["15:00","16:00"],["16:00","17:00"]]
2. Judgment of time interval including critical point
What if the given time interval contains critical time points, such as 10:00-17:00 ?
console.log(JSON.stringify(judge('10:00', '17:00')))
The output at this time is as follows:
[["10:00","11:00"],["11:00","12:00"],["12:00","13:00"],["13:00","14:00"],["14:00","15:00"],["15:00","16:00"],["16:00","17:00"]]
In practical applications, there may be different rules for critical point time, and there are the following three scenarios:
(1) Counting two time intervals at the same time, such as 16:00, is not only counted as ["15:00", "16:00"], but also counted as ["16:00", "17:00"] ] interval;
(2) The critical time is used as the end time, such as 16:00, then it is only counted as being in the ["15:00", "16:00"] interval;
(3) The critical time is used as the starting time, such as 16:00, then it is only counted as being in the ["16:00", "17:00"] interval;
If you want to satisfy the above three scenarios at the same time, you need to modify the judgment method and control it by passing in the corresponding parameters. The modified code is as follows:
function judge(startTime, endTime, leftEquals, rightEquals) {
// 生成24小时时间区间,跨度为1小时
let timeArrays = new Array(24).fill(['', '']).map((item, index) => [(index < 10 ? '0' + index : index) + ':00', ((index + 1) < 10 ? '0' + (index + 1) : (index + 1)) + ':00']);
return timeArrays.filter(item => !((compare(item[0], startTime, leftEquals) && compare(item[0], endTime, rightEquals)) || (compare(startTime, item[1], leftEquals) && compare(endTime, item[1], rightEquals))));
}
function compare(startTime, endTime, equals) {
// 将时间转换为分钟,再进行比较
let startTimes = startTime.split(':');
let endTimes = endTime.split(':');
let startTimeVal = startTimes[0] * 60 + Number(startTimes[1]);
let endTimeVal = endTimes[0] * 60 + Number(endTimes[1]);
return equals ? startTimeVal >= endTimeVal : startTimeVal > endTimeVal;
}
Let's test the above three scenarios respectively:
Scenario 1 : Both sides are included. Since it is a negation judgment method, both parameters need to be passed false
console.log(JSON.stringify(judge('10:00', '12:00', false, false)))
The output is as follows:
[["09:00","10:00"],["10:00","11:00"],["11:00","12:00"],["12:00","13:00"]]
Scenario 2 : Tipping point as end time
console.log(JSON.stringify(judge('10:00', '12:00', false, true)))
The output is as follows:
[["09:00","10:00"],["10:00","11:00"],["11:00","12:00"]]
Scenario 3 : The critical point as the starting time
console.log(JSON.stringify(judge('10:00', '12:00', true, false)))
The output is as follows:
[["10:00","11:00"],["11:00","12:00"],["12:00","13:00"]]
3. Judgment that the time interval spans 0:00
In the two cases discussed above, the problem that a given time interval spans 0 o'clock is not considered, that is, the case where the end time is less than the start time, such as 10:15-01:23 , then for this case, What needs to be done?
If the time interval spans 0:00, it means that there are two time intervals, namely 10:15-24:00 and 00:00-01:23. At this time, the two time intervals are judged, and the modified code as follows:
function judge(startTime, endTime, leftEquals, rightEquals) {
// 分割时间区间,判定结束时间是否小于起始时间
let targetTimes = compare(startTime, endTime, false) ? [[startTime, '24:00'], ['00:00', endTime]] : [[startTime, endTime]];
// 生成24小时时间区间,跨度为1小时
let timeArrays = new Array(24).fill(['', '']).map((item, index) => [(index < 10 ? '0' + index : index) + ':00', ((index + 1) < 10 ? '0' + (index + 1) : (index + 1)) + ':00']);
return timeArrays.filter(item => targetTimes.some(target => !((compare(item[0], target[0], leftEquals) && compare(item[0], target[1], rightEquals))
|| (compare(target[0], item[1], leftEquals) && compare(target[1], item[1], rightEquals)))));
}
function compare(startTime, endTime, equals) {
// 将时间转换为分钟,再进行比较
let startTimes = startTime.split(':');
let endTimes = endTime.split(':');
let startTimeVal = startTimes[0] * 60 + Number(startTimes[1]);
let endTimeVal = endTimes[0] * 60 + Number(endTimes[1]);
return equals ? startTimeVal >= endTimeVal : startTimeVal > endTimeVal;
}
Let's verify the time interval of 10:15-01:23
console.log(JSON.stringify(judge('10:15', '01:23', false, true)))
The output is as follows
[["00:00","01:00"],["01:00","02:00"],["10:00","11:00"],["11:00","12:00"],["12:00","13:00"],["13:00","14:00"],["14:00","15:00"],["15:00","16:00"],["16:00","17:00"],["17:00","18:00"],["18:00","19:00"],["19:00","20:00"],["20:00","21:00"],["21:00","22:00"],["22:00","23:00"],["23:00","24:00"]]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。