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

eqzww0vc  于 2021-09-13  发布在  Java
关注(0)|答案(3)|浏览(265)

我得到了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; }
    }
qmb5sa22

qmb5sa221#

一般来说,在处理数组时最好使用循环,而不是ifs块或switch/case之类的东西。好处很多,但特别是在循环中运行逻辑可以
写一次。
有任意数量的案例。
我不确定 hrsmins 在项目中指定为不相关的数组,或者这是在此步骤之前执行的步骤。最好把时间和分钟放在一起。特别是,如果 hrs 最后有一个额外的条目 mins 没有。我编写的示例假设将间隔作为字符串数组(如“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")));

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

uqzxnwby

uqzxnwby2#

由于只需要考虑一天中的时间,我们可以通过计算而不使用完整日期对象。我将基于字符串的间隔转换为数字并存储在 intv . 要测试的时间值( dt )然后将小时数部分乘以100,再加上分钟数(变量 n ). 其余的工作在 while 循环,我穿过间隔,直到到达一个 intv[p] 大于的值 n (或直到我到达数组的末尾: 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);
}
von4xj4u

von4xj4u3#

我想提供一个使用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: " + periods[periodStart] + " - " + periods[periodEnd]);

相关问题