Home > OS >  How do I get an array of times with a start hour, end hour?
How do I get an array of times with a start hour, end hour?

Time:10-21

function getTimesArray(start, end, length) {
    let startMin = start * 60
    let endMin = end * 60
    let times = []

    while (startMin <= endMin){
        let mins = startMin % 60
        let hours = Math.floor(startMin / 60)
        let timeString = hours.toString()   ":"    mins.toString().padStart(2, '0')
        times.push(timeString)
        startMin  = length
    }
    return times
}
console.log(getTimesArray(09,11, 35))
output ["9:00", "9:35", "10:10", "10:45"]

I need to pass the minutes as an argument. Like this console.log(getTimesArray(09:12,11:25,35)). Could someone help me?

CodePudding user response:

function getTimesArray(start, end, length) {
  let startMin = (parseInt(start.split(':')[0]) * 60)   parseInt(start.split(':')[1]);
  let endMin = (parseInt(end.split(':')[0]) * 60)   parseInt(end.split(':')[1]);
  let times = [];

  while (startMin <= endMin){
    let mins = startMin % 60
    let hours = Math.floor(startMin / 60)
    let timeString = hours.toString()   ":"    mins.toString().padStart(2, '0')
    times.push(timeString)
    startMin  = length
  }
  return times
}

console.log(getTimesArray('09:12','11:00', 35));

  • Related