我在年中运行良好,但现在我们已经过渡到新的一年,我的代码中断了。我需要获取前 6 个月的月份名称和年份。
我修改后的代码:
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);
有人可以帮我解决这个问题吗?
最佳答案
你可以做这样的事情
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
关于javascript - 列出过去 6 个月的月份名称和年份,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27844502/