我在今年年中的工作正常,但现在我们已经过了新的一年,我的代码断了。我需要获得前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);
有人能帮我一把吗?
答案 0 :(得分:7)
你可以做这样的事情
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);
}
答案 1 :(得分:2)
我将在@zynetro提供的答案上进一步扩展以包括年份。
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var today = new Date();
var d;
var month;
var year;
for(var i = 6; i > 0; i -= 1) {
d = new Date(today.getFullYear(), today.getMonth() - i, 1);
month = monthNames[d.getMonth()];
year = d.getFullYear();
console.log(month);
console.log(year);
}
以下是fiddle。
答案 2 :(得分:1)
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 {
if (month < 0) {
month = 11;
year--;
}
$("#pickWinners_month").append($("<option />").val((month > 9 ? "" : "0") + month + "/01/" + year).html(monthNames[month] + " " + year));
month--;
i++;
} while (i < 6);
这是一个有效的Sample
答案 3 :(得分:0)
正如zaynetro所说,你有错误的月份指数:
以下是完整代码:
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 > 9 ? "" : "0") + month + "/01/" + year).html(monthNames[month] + " " + year));
if (month == 0) {
month = 11;
year--;
} else {
month--;
}
i++;
} while (i < 6);
答案 4 :(得分:0)
您可以这样做:
$date ='2015-01-01'
for (int i=0;i<6; i++){
$datemonthBefore = date('Y-m-d',strtotime('-i months', strtotime($date)));
var month = datemonthBefore.getMonth();
}
答案 5 :(得分:0)
看起来我明白了。从今天起减去一个月的日期,然后将其用作新的开始日期。
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var lastMonth = new Date();
lastMonth.setMonth(lastMonth.getMonth() - 1);
var year = lastMonth.getFullYear();
var month = lastMonth.getMonth();
var i = 0;
do {
$("#pickWinners_month").append($("<option />").val((month > 9 ? "" : "0") + month + "/01/" + year).html(monthNames[month] + " " + year));
if (month == 0) {
month = 11;
year--;
} else {
month--;
}
i++;
} while (i < 6);
答案 6 :(得分:0)
const today = new Date();
let lastSixMonths = []
for (var i = 6; i > 0; i -= 1) {
const date = new Date(today.getFullYear(), today.getMonth() - i, 1);
lastSixMonths.push(moment(date).format("MMMM YYYY"))
}
return lastSixMonths.reverse() // Result