【发布时间】:2021-07-26 18:59:43
【问题描述】:
我得到了 6 个小时和分钟的时间戳,它们定义了一段时间,我们称它们为 1 到 6。 (例如:03:13、05:22、12:54、16:55、20:23、22:14)
例如:第一节从 03:13 开始,一直持续到 05:22 -1 分钟,以此类推
当然,最后,月经必须涵盖一天中的所有 24 小时,因此第六个月经(从 22:14 开始)将持续到 03:13
编写一个可以告诉我们当前处于什么时间段的函数的最佳/最佳方法是什么?
这是我的尝试(不起作用并且不是最理想的):
function currentPeriod(hrs, mins, now) {
// hrs is an array of integers and they define only hours (3, 5, 12,...)
// mins is an array of integers and they define minutes (13, 22, 54,...)
// now is a JavaScript Date
if(now.getHours() > 0 && now.getHours() < hrs[0]) { return 6; }
else if(now.getHours() == hrs[0] && now.getMinutes() < mins[0]) { return 6; }
else if(now.getHours() == hrs[0] && now.getMinutes() >= mins[0]) { return 1; }
else if(now.getHours() > hrs[0] && now.getHours() < hrs[1]) { return 1; }
else if(now.getHours() == hrs[1] && now.getMinutes() < mins[1]) { return 1; }
else if(now.getHours() == hrs[1] && now.getMinutes() >= mins[1]) { return 2; }
else if(now.getHours() > hrs[1] && now.getHours() < hrs[2]) { return 2; }
else if(now.getHours() == hrs[2] && now.getMinutes() < mins[2]) { return 2; }
else if(now.getHours() == hrs[2] && now.getMinutes() >= mins[2]) { return 3; }
else if(now.getHours() > hrs[2] && now.getHours() < hrs[3]) { return 3; }
else if(now.getHours() == hrs[3] && now.getMinutes() < mins[3]) { return 3; }
else if(now.getHours() == hrs[3] && now.getMinutes() >= mins[3]) { return 4; }
else if(now.getHours() > hrs[3] && now.getHours() < hrs[4]) { return 4; }
else if(now.getHours() == hrs[4] && now.getMinutes() < mins[4]) { return 4; }
else if(now.getHours() == hrs[4] && now.getMinutes() >= mins[4]) { return 5; }
else if(now.getHours() > hrs[4] && now.getHours() < hrs[5]) { return 5; }
else if(now.getHours() == hrs[5] && now.getMinutes() < mins[5]) { return 5; }
else if(now.getHours() == hrs[5] && now.getMinutes() >= mins[5]) { return 6; }
else if(now.getHours() <= 23 && now.getMinutes() <= 59) { return 6; }
}
【问题讨论】:
-
嗨 BakirGracic,以下答案之一是否解决了您的问题?如果是这样,请使用绿色复选标记接受其中之一。它让回答者知道您的问题已经解决。如果没有,请详细说明您仍然遇到的问题。
标签: javascript jquery date