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

206
Visualizações
How to get previous Six months into an array

I am trying to use moment to get the previous six months, using the below code.

ngOnInit(){
let mom: moment.Moment;
var d = new Date();
let numberOfMonths=6;
let previousSIxMonths:string[]=[];

while (this.numberOfMonths > 0) {
  this.previousSixMonths.push(mom.subtract(this.numberOfMonths, 'months').format('MMMM'));

  this.numberOfMonths--;
  console.log(this.numberOfMonths);
}
console.log(this.previousSixMonths);
}

However these cards should be displayed whenever I am on the page, and hence I have written the above code inside OnInit. Once I have all the past six months in the array, the idea is to loop over the array and display in cards. But the doesn't really work for me.

I want all my previous six months to be in an array so that I could loop across the array.

Considering this is October month, the expected output should be:

["Oct","Sept","Aug","July","June","May"]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here is a quick way to do what you're needing:

var previousSixMonths = function(){
  let n = 0;
  let arRet = [];
  
  for(; n < 6; n++)
    arRet.push(moment().subtract(n, 'months').format('MMMM'));
  return(arRet)
}();

console.log(previousSixMonths);
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