从开始和结束时间获取时隙

时间:2019-07-17 05:49:25

标签: javascript

我想要从给定时间间隔的开始时间和结束时间开始的时隙。还需要检查如果提供的日期与今天的日期相同,则开始时间将是特定区域的当前时间。(例如,如果时间是10:12,那么我们需要从10:30开始,如果时间是10:36,那么选择11:00(即从1分钟到29,我们应该选择30分钟,从31分钟到59,我们应该选择00))。 那么,如何使用下面的给定值实现这一目标?

输入:

let date = "20-07-2019" 
let startTime = "10:00";
let endtime = "14:00";
let interval = 60; // in minutes

预期输出:

["10:00 - 11:00", "11:00- 12:00" , "12:00- 13:00", "13:00 - 14:00"]

我的代码:

let parseTime = (s) => {
  let c = s.split(':');
  return parseInt(c[0]) * 60 + parseInt(c[1]);
}

let convertHours = (mins) => {
  let hour = Math.floor(mins / 60);
  mins = mins % 60;
  let converted = pad(hour, 2) + ':' + pad(mins, 2);
  return converted;
}

let pad = (str, max) => {
  str = str.toString();
  return str.length < max ? pad("0" + str, max) : str;
}

let calculate_time_slot = (start_time, end_time, interval) => {
  let i, formatted_time;
  let time_slots = new Array();
  for (let i = start_time; i <= end_time; i = i + interval) {
    formatted_time = convertHours(i);
    time_slots.push(formatted_time);
  }
  return time_slots;
}

let date = "20-07-2019"
let startTime = "10:00";
let endTime = "14:00";
let interval = 60; // in minutes
start_time = parseTime(startTime)
end_time = parseTime(endTime)
let times_ara = calculate_time_slot(start_time, end_time, interval);

console.log(times_ara);

3 个答案:

答案 0 :(得分:1)

您的函数似乎生成了所需的值,您只需要更改它们的表示方式即可,这是一种实现方法:

let time_slots = ["10:00", "11:00", "12:00", "13:00", "14:00"];

time_slots = time_slots.reduce((a, c, i, arr) => {
  if (i < arr.length - 1) {
    a.push(`${c} - ${arr[i + 1]}`);
  }
  return a;
}, []);

console.log(time_slots);

答案 1 :(得分:1)

我将for循环更改为一会儿,然后在循环中增加间隔,以便您在循环中同时拥有开始时间和结束时间,例如

let parseTime = (s) => {
  let c = s.split(':');
  return parseInt(c[0]) * 60 + parseInt(c[1]);
}

let convertHours = (mins) => {
  let hour = Math.floor(mins / 60);
  mins = mins % 60;
  let converted = pad(hour, 2) + ':' + pad(mins, 2);
  return converted;
}

let pad = (str, max) => {
  str = str.toString();
  return str.length < max ? pad("0" + str, max) : str;
}

let calculate_time_slot = (start_time, end_time, interval) => {
  let i, formatted_time;
  let time_slots = new Array();
  // Round start and end times to next 30 min interval
  start_time = Math.ceil(start_time / 30) * 30;
  end_time = Math.ceil(end_time / 30) * 30;

  // Start and end of interval in the loop
  while (start_time < end_time) {
    let t = convertHours(start_time) + ' - ' + (convertHours(start_time += interval));
    time_slots.push(t);
  }
  return time_slots;
}

let date = "20-07-2019"
let startTime = "10:00";
let endtime = "14:00";
let interval = 60; // in minutes
let end_time = parseTime(endtime)
let start_time = parseTime(startTime)

let times_ara = calculate_time_slot(start_time, end_time, interval);

console.log(times_ara);

我也要更改很多其他内容,但这是获得所需结果的最低要求。

哦,我还想对下一个30分钟的间隔进行调整,因为这也是您所说的。

答案 2 :(得分:1)

这是我得到的最短的

let calculate_time_slot = (start_time, end_time, interval) => {
  const timeSlots = [];
  for (let i = start_time; i < end_time; i += interval) {
    const formattedBegin = convertHours(i);
    const formattedEnd = convertHours(i + interval);
    timeSlots.push(formattedBegin + ' - ' + formattedEnd);
  }
  return timeSlots;
}

可运行代码段:

let parseTime = (s) => {
  let c = s.split(':');
  return parseInt(c[0]) * 60 + parseInt(c[1]);
}

let convertHours = (mins) => {
  let hour = Math.floor(mins / 60);
  mins = mins % 60;
  let converted = pad(hour, 2) + ':' + pad(mins, 2);
  return converted;
}

let pad = (str, max) => {
  str = str.toString();
  return str.length < max ? pad("0" + str, max) : str;
}

let calculate_time_slot = (start_time, end_time, interval) => {
  const timeSlots = [];
  for (let i = start_time; i < end_time; i += interval) {
    const formattedBegin = convertHours(i);
    const formattedEnd = convertHours(i + interval);
    timeSlots.push(formattedBegin + ' - ' + formattedEnd);
  }
  return timeSlots;
}

let date = "20-07-2019"
let startTime = "10:00";
let endTime = "14:00";
let interval = 60; // in minutes
start_time = parseTime(startTime)
end_time = parseTime(endTime)
let times_ara = calculate_time_slot(start_time, end_time, interval);

console.log(times_ara);