gpt4 book ai didi

javascript - 列出过去 6 个月的月份名称和年份

转载 作者:行者123 更新时间:2023-12-01 02:00:27 25 4
gpt4 key购买 nike

我在年中的时候工作得很好,但现在我们已经进入新的一年了,我的代码崩溃了。我需要获取前 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com