How to get the current month and based on the month get the last two months in Angular 2?
I get the current month like this:
private _createMonthList(): any {
var d = new Date();
d.setMonth(d.getMonth());
console.log('MONTH', d);
}
But what i wanted is a list like:
[May-2017, April-2017, March-2017]
. Any idea guys?
Use getUTCMonth to get month index and make a array month with name to get the month name from index
var month= ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var date =new Date();
var n = date.getUTCMonth();
console.log(month[n] + ' ' +date.getUTCFullYear());
if(n != 0){
console.log(month[n-1] + ' ' +date.getUTCFullYear());
console.log(month[n-2] + ' ' +date.getUTCFullYear());
}else{
console.log(month[11] + ' ' +date.getUTCFullYear());
console.log(month[10] + ' ' +date.getUTCFullYear());
}