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

169
Visualizações
Convert an array of dates into a date range containing Month, days, hours accumulated in JavaScript

I have a list of dates that I need to convert into a date range, slicing out the Month values that aren't on the first date of the date ranges, while also adding the hours accumulated for the days within the day portion.

My example array is:

   ["Nov 23 2021 8 hrs",
    "Nov 24 2021 8 hrs",
    "Nov 27 2021 8 hrs",
    "Dec 3 2021 8 hrs"]

I am trying to get the end array to look like this:

["Nov 23-24 2021 16hrs, Nov 27 2021 8hrs, Dec 3 2021 8hrs"]

So far I was able to get the dates to concatenate correctly with this code just passing in an array that contains only numbers

 const getDayRanges = (dayArr) => {
  var ranges = [],
    rstart,
    rend;
  for (var i = 0; i < dayArr.length; i++) {
    console.log("days" ,dayArr[i])
    rstart = dayArr[i];
    rend = rstart;
    while (dayArr[i + 1] - dayArr[i] == 1) {
      rend = dayArr[i + 1]; // increment the index if the numbers sequential
      i++;
    }
    ranges.push(rstart == rend ? rstart + "" : rstart + " to " + rend);
  }
  return ranges;
};

But I have not been about to figure out how to slice out the Months correctly to place them in a new array with the hyphenated date numbers, let alone how to then add up all of the hours portion that align with the dates that are concatenated.

Any help would be extremely appreciated.

about 4 years ago · Juan Pablo Isaza
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