node.js get dates of last 12 months

This function gets the dates of last 12 months.
[code language=”javascript”]
var last12Month = function() {
var dates = [];
d = new Date(),
y = d.getFullYear(),
m = d.getMonth();
function padMonth(month){
if(month<10){
return ‘0’+month;
} else {
return month;
}
}
if( m === 11 ) {
for(var i=1; i<13; i++) {

dates.push(y+"-"+ padMonth(i) +"-01");
}
} else {
for(var i=m+1; i<m+13; i++) {
if( (i%12) > m ) {
dates.push( (y-1) + "-" + padMonth(i+1) + "-01" );
} else {
dates.push( y + "-" + padMonth((i%12)+1) + "-01" );
}
}
}

return dates;
}
console.log(last12Month());
[/code]

If the current month is Jan 2015, the output will be:
[code language=”text”]
[ ‘2014-02-01’,
‘2014-03-01’,
‘2014-04-01’,
‘2014-05-01’,
‘2014-06-01’,
‘2014-07-01’,
‘2014-08-01’,
‘2014-09-01’,
‘2014-10-01’,
‘2014-11-01’,
‘2014-12-01’,
‘2015-01-01’ ]
[/code]

Search within Codexpedia

Custom Search

Search the entire web

Custom Search