Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!