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

136
Visualizações
Get array of dates between two dates with custom interval

I have this data:

const start = "29.09.2021";
const end = "29.10.2021";
const interval = "days"; //also: "month", "week", "year"
const intervalCount = 3;

how to get an array of dates that exists between start and end with intervals: eq. if interval == "days" and intervalCount == 3 then another dates should be 02.10, 05.10, 08.10, 11.10 etc., but interval can be also "month", "week", and "year" so I must calculate based on the dynamic arguments

I have no idea how to even start, thanks for any help!

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

0

This can be done using Date objects found here

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

You can create a function such as

function getDates(startDateString, stopDateString, interval){
  var dates = [];
  var start = new Date(startDateString);
  var stop = new Date(stopDateString);

  while (start < stop) {
    dates.push (start.toJSON());
    start.setDate(start.getDate() + interval);
  }
  return dates;
}

And then you can run the function by making this call

dateArray = getDates("09-29-2021", "10-29-2021", 1)

It covers single day iterations. A week is just a 7 day interval.

dateArray = getDates("09-29-2021", "10-29-2021", 7)

Moving months gets into edge cases that can be handled through conditions based on your use case (For example, if the date is Jan 31st, should a jump of 1 month take you to Mar 3rd or Feb 28th/29th?)

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