Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

235
Visualizações
Starting time in the program is not working properly

I have a function returnTimesInBetween. It's taking two values (start, end) as parameters and returns the time array by splitting in the 30 minutes of the time frame. the problem is when the start time is ending with 30 minutes like "12:30,11:30,15:30" it's not taking that value in the time frame and when the start time is like 11:00, 12:00, etc it's working fine.

//DIVIDING THE TIME FRAME IN 30  MINUTES OF THE TIME FRAME.
function returnTimesInBetween(start, end) 
{
    var timesInBetween = ([]);
    console.log(timesInBetween);
    var startH = parseInt(start.split(":")[0]);
    var startM = parseInt(start.split(":")[1]);
    var endH = parseInt(end.split(":")[0]);
    var endM = parseInt(end.split(":")[1]);
    if (startM == 30)
        startH++;
    for (var i = startH ; i < endH; i++) {
        timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00")
        timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30");
    }
    if (endM == 00)
        timesInBetween.push(endH + ":30");
    if (endM == 30)
        timesInBetween.push(endH + ":30")
}
console.log('time range:-')
console.log(returnTimesInBetween("11:30", "15:30"));
console.log(returnTimesInBetween("18:00", "21:30"));

console output are

    []0: "12:00"
      1: "12:30"
      2: "13:00"
      3: "13:30"
      4: "14:00"
      5: "14:30"
      6: "15:30"
      length: 7[[Prototype]]: Array(0)

       []0: "18:00"
       1: "18:30"
       2: "19:00"
       3: "19:30"
       4: "20:00"
       5: "20:30"
       6: "21:30"
       length: 7[[Prototype]]: Array(0)

How can i add the starting time even if it's ending with 30 minute hand.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your condition on the for loop should be i <= endH; and you can use th if condition according to your need of the output.

for (var i = startH ; i <= endH; i++) {

From what i understand your expected output is:

("11:30", "15:30") => ["11:00","11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","15:30"]

("18:00", "21:30") => [ "18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30" ]

//DIVIDING THE TIME FRAME IN 30  MINUTES OF THE TIME FRAME.
function returnTimesInBetween(start, end) 
{
var timesInBetween = ([]);
console.log(timesInBetween);
var startH = parseInt(start.split(":")[0]);
var startM = parseInt(start.split(":")[1]);
var endH = parseInt(end.split(":")[0]);
var endM = parseInt(end.split(":")[1]);

for (var i = startH ; i <= endH; i++) {
    timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00");
    (i == endH && endM == 30) ? timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30") : '';
}

return timesInBetween;
}
console.log('time range:-')
console.log(returnTimesInBetween("11:30", "15:00"));
console.log(returnTimesInBetween("11:30", "15:30"));
console.log(returnTimesInBetween("18:00", "21:30"));

Or

if you are expecting somthing like this, just add the if condition:

["11:30","12:00","12:30","13:00","13:30","14:00","14:30","15:00","15:30"]
["18:00","18:30","19:00","19:30","20:00","20:30","21:00","21:30"]

//DIVIDING THE TIME FRAME IN 30  MINUTES OF THE TIME FRAME.
function returnTimesInBetween(start, end) 
{
var timesInBetween = ([]);
console.log(timesInBetween);
var startH = parseInt(start.split(":")[0]);
var startM = parseInt(start.split(":")[1]);
var endH = parseInt(end.split(":")[0]);
var endM = parseInt(end.split(":")[1]);

if (startM == 30) {
    timesInBetween.push(startH < 10 ? "0" + startH + ":30" : startH + ":30");
    startH++;
}
    
for (var i = startH ; i <= endH; i++) {
    timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00");
    (i == endH && endM == 30) ? timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30") : '';
}

return timesInBetween;
}
console.log('time range:-')
console.log(returnTimesInBetween("11:30", "15:00"));
console.log(returnTimesInBetween("18:00", "21:30"));

about 4 years ago · Juan Pablo Isaza Relatório

0

You need a condition just for the first iteration, when starM is not 30.

//DIVIDING THE TIME FRAME IN 30  MINUTES OF THE TIME FRAME.
function returnTimesInBetween(start, end) {
  var timesInBetween = [];
  var startH = parseInt(start.split(":")[0]);
  var startM = parseInt(start.split(":")[1]);
  var endH = parseInt(end.split(":")[0]);
  var endM = parseInt(end.split(":")[1]);

  for (var i = startH; i < endH; i++) {
    // Add a condition here for the first iteration only
    if (i === startH && startM !== 30) {
      timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00");
    }
    timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30");
  }
  if (endM == 0) timesInBetween.push(endH + ":30");
  if (endM == 30) timesInBetween.push(endH + ":30");

  return timesInBetween;  // Was missing ;)
}

console.log(returnTimesInBetween("11:30", "15:30"));
console.log(returnTimesInBetween("18:00", "21:30"));

about 4 years ago · Juan Pablo Isaza Relatório

0

Another approach I like when working with time, instead of working with hour, minute (and second) separately, crunch them into a single number (in this case minutes) and do all calculation with the number. Only convert back to human-readable format when finish computing. This way you avoid making mistake when changing one component and forget the other. This applies to other non-base 10 system as well.

function returnTimesInBetween(start, end) 
{
  let result = [];
  const s = start.split(":").reduce((a,c) => a * 60 + Number(c));
  const e = end.split(":").reduce((a,c) => a * 60 + Number(c));
  const mod = (s % 30);
  for(let i = s + (mod ? 30 - mod : 0) ; i <= e; i += 30)
  {
    const h = Math.floor(i/60);
    const m = i % 60;
    result.push(`0${h}`.slice(-2) + ":" + `0${m}`.slice(-2) );
  }
  return result;
}

console.log(returnTimesInBetween("11:00","15:29"));
console.log(returnTimesInBetween("11:00","15:30"));
console.log(returnTimesInBetween("11:29","15:30"));
console.log(returnTimesInBetween("11:30","15:30"));
console.log(returnTimesInBetween("11:31","15:30"));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda