There is such a scenario: given a time, it is necessary to determine which time range this time is in.
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 of 15:28 , then you need to return the time range of ["15:00", "16:00"] . The specific implementation code is as follows:
function judge(time) {
// 生成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(time, item[0]) && compare(item[1], time));
}
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;
}
Test it, incoming time 15:28
console.log(judge('15:28'));
The result returned after execution is as follows:
[["15:00","16:00"]]
If you pass in the time of the critical point, such as 16:00 , what is the result?
console.log(judge('16:00'));
The result returned after execution is as follows:
[["15:00","16:00"],["16:00","17:00"]]
In practical application scenarios, for the critical point time, how to divide which interval it is located in, there are usually the following situations:
(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 be compatible with the above situations at the same time, you need to modify the judgment comparison method and control it through the corresponding parameters. The specific modified code is as follows:
function judge(time, 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(time, item[0], leftEquals) && compare(item[1], time, 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 separately:
scene one:
console.log(judge('16:00', true, true));
The output is as follows:
[["15:00","16:00"],["16:00","17:00"]]
Scenario two:
console.log(judge('16:00', false, true))
The output is as follows:
[["15:00","16:00"]]
Scenario three:
console.log(judge('16:00', true, false))
The output is as follows:
[["16:00","17:00"]]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。