I had this working fine for the middle of the year but now that we've rolled over to a new year, my code breaks. I need to get the month name and year for the previous 6 months.
My modified code:
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth();
var i = 0;
do {
$("#pickWinners_month").append($("<option />").val((month > 8 ? "" : "0") + month + "/01/" + year).html(monthNames[month - 1] + " " + year));
if (month == 0) {
month = 11;
year--;
} else {
month--;
}
i++;
} while (i < 6);
Can someone give me a hand with this?
You could do something like this
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var today = new Date();
var d;
var month;
for(var i = 6; i > 0; i -= 1) {
d = new Date(today.getFullYear(), today.getMonth() - i, 1);
month = monthNames[d.getMonth()];
console.log(month);
}
Run code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With