Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda