如何找到给定时间跨度的哪个时间段是当前的



我已经得到了6个小时和分钟的时间戳,它们定义了一段时间,我们称它们为1到6。(例如:03:13 05:22 12:54,16,20:23,二二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; }
}

一般来说,处理数组时最好使用循环,而不是if块或switch/case之类的东西。这样做的好处很多,但特别的是,在循环中运行逻辑允许您

  • 写一次
  • 有任意数量的病例。

我不确定您的项目中的hrsmins是否被指定为不相关的数组,或者这是您在此步骤之前采取的步骤。把小时和分钟放在一起比较好。特别是,如果hrsmins没有的额外条目结束,那么很容易破坏您的代码。在编写示例时,我假设将间隔作为字符串数组(如"23:43")传入是相当容易的。如果没有该字符串,代码中的注释提供了构建该字符串的建议方法。

function currentPeriod(intervals, now) {
// Break these out so the methods are only called once.
const nowHour = now.getHours();
const nowMinute = now.getMinutes();

// Iterate over all the intervals. If having the intervals broken into separate
// arrays is a requirement of the project, they should be recombined like this:
// ```
// const intervals = hrs.map((h, i) => `${h}:${mins[i]}`);
// ```
for (let i = 0; i < intervals.length; i++) {
// Get the hour and minute of the end of the interval.
const [hour, minute] = intervals[i].split(":").map(a => +a);
if (hour > nowHour) {
// If the hour is after the current time, this is the correct interval. 
return i;
}
else if (hour === nowHour && minute > nowMinute) {
// If the hour is the same as the current time, and the minute is after
// the current time, this is the correct interval.
return i;
}
}
// If the current time is after the last interval, say it's in the first
// interval.
return 0;
}
const intervals = ["03:13", "05:22", "12:54", "16:55", "20:23", "22:14"];
console.log("00:00", currentPeriod(intervals, new Date("2020-01-01T00:00:00")));
console.log("03:13", currentPeriod(intervals, new Date("2020-01-01T03:13:00")));
console.log("12:59", currentPeriod(intervals, new Date("2020-01-01T12:59:00")));
console.log("22:15", currentPeriod(intervals, new Date("2020-01-01T22:15:00")));

注意:它只需要查看当前时间是否在下一个间隔之前,因为如果它在前一个间隔之前,循环将在那个间隔上退出。这节省了一半的解决方案的复杂性,因为事情不需要检查两次。

因为只需要考虑一天中的时间,所以我们可以通过不使用完整的Date对象进行计算。我将基于字符串的间隔转换为数字并将它们存储在intv中。要测试的时间值(dt)然后可以通过将小时部分乘以100并加上分钟(变量n)将其转换为整数。其余的是在while循环中完成的,在这里我遍历间隔,直到我到达一个大于nintv[p]值(或者直到我到达数组的末尾:p<int.length将是false)。模运算(%)是必要的,因为对于n的值大于最后一个intv的值,我希望周期数p再次为0(而不是像本例中那样为6)。

function period(date){
const intv = ["03:13", "05:22", "12:54", "16:55", "20:23", "22:14"].map(t=>+t.replace(":",""));
let p=0,n=100*date.getHours()+date.getMinutes();
while(n>=intv[p] && p<intv.length) p++
return p%intv.length;
}
// testing:
let txt,dt=new Date();
for (i=0;i<48;i++){
txt=dt.toLocaleString("de-DE").substr(-8)+"h: period ";
console.log(txt+period(dt));
dt.setMinutes(dt.getMinutes()+30);
}

我想提供一个使用DateTime对象并具有可读代码的答案。

let periods = ["03:13", "05:22", "12:54", "16:55", "20:23", "22:14"];
let periodDateTimeObjects = [];
periods.forEach(period => {
let hours = Number(period.slice(0,2));
let minutes = Number(period.slice(-2));
periodDateTimeObjects.push(new Date().setUTCHours(hours, minutes, 0, 0));
});
let currentDateTime = new Date();
let periodStart = 0;
periodDateTimeObjects.forEach((periodDateTime, index) => {
if (currentDateTime > periodDateTime) { periodStart = index; } 
});
let periodEnd = periodStart + 1;
if (periodEnd == periods.length) { periodEnd = 0; }
console.log("Time is now: " + currentDateTime.getHours() + ":" + currentDateTime.getMinutes() + " and current period is " + Number(periodStart + 1) + ": " + periods[periodStart] + " - " + periods[periodEnd]);

相关内容

  • 没有找到相关文章

最新更新