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

83
Visualizações
Display months between two dates with different years

I have an array of months:

const monthsArr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];  

Also I have two dates: 2021-09-07 and 2022-03-17 . To get the number of the months between these dates I use function:

function monthDiff(dateFrom, dateTo) {
    return dateTo.getMonth() - dateFrom.getMonth() + 12 * (dateTo.getFullYear() - dateFrom.getFullYear());
}

How to display all months from the array monthsArr between those two dates? Thanks in advance for any help.

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

0

You might find useful to use a utility library such as date-fns (https://date-fns.org/), more specifically the sub function (https://date-fns.org/v2.28.0/docs/sub)

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use Array().fill() to generate all 12 months for all involved years, flatten that, and then slice the relevant part from it. To make sure that the second argument of slice will be a negative (not zero!), even add 12 more months to that. The second argument will then be a value between -23 and -12:

const monthsArr = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];  

function monthsBetween(dateFrom, dateTo) {
    return Array(dateTo.getFullYear() - dateFrom.getFullYear() + 2)
           .fill(monthsArr).flat()
           .slice(dateFrom.getMonth(), dateTo.getMonth() - 23);
}

let dateFrom = new Date("2021-09-07");
let dateTo = new Date("2022-03-17");

console.log(monthsBetween(dateFrom, dateTo));

about 4 years ago · Juan Pablo Isaza Relatório

0

A simplistic approach is to increment the start date by one month at a time and return the value at that index from the array. The only issue is to work out whether "between" includes the start and end month or only those between those months.

The following includes the start and end months in the result:

let months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

function getMonthsBetweenDates(startDate, endDate) {
  // Make start as 1st of startDate month 
  let s = new Date(startDate.getFullYear(), startDate.getMonth());
  let result = [];
  // Loop over months until get to end month
  while (s <= endDate) {
    result.push(months[s.getMonth()]);
    s.setMonth(s.getMonth() + 1);
  }
  return result;
}

// Get months between 2021-09-07 and 2022-03-17
console.log(getMonthsBetweenDates(new Date(2021,8,7), new Date(2022,2,17)));

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