根据当月,我尝试获得上个月。但问题发生在"年"不是2017年。
那么我怎样才能获得上一年的月份?下面的代码将描述我想要的内容,如果有人知道如何获取它,请告诉我方法。谢谢:))
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var cur_month = new Date();
var cur_month_now = month[cur_month.getMonth()];
var pre_month_1 = month[cur_month.getMonth()-1];
var pre_month_2 = month[cur_month.getMonth()-2];
var pre_month_3 = month[cur_month.getMonth()-3];
var pre_month_4 = month[cur_month.getMonth()-4];
var pre_month_5 = month[cur_month.getMonth()-5];
document.getElementById("cur_month").innerHTML = cur_month_now;
document.getElementById("pre_month_1").innerHTML = pre_month_1;
document.getElementById("pre_month_2").innerHTML = pre_month_2;
document.getElementById("pre_month_3").innerHTML = pre_month_3;
document.getElementById("pre_month_4").innerHTML = pre_month_4;
document.getElementById("pre_month_5").innerHTML = pre_month_5;

<div class="dropdown">
<button class="btn btn-danger dropdown-toggle" type="button" data-toggle="dropdown">Select Month
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#" id="cur_month"></a></li>
<li><a href="#" id="pre_month_1"></a></li>
<li><a href="#" id="pre_month_2"></a></li>
<li><a href="#" id="pre_month_3"></a></li>
<li><a href="#" id="pre_month_4"></a></li>
<li><a href="#" id="pre_month_5"></a></li>
</ul>
</div>
&#13;
答案 0 :(得分:5)
由于month[-1]
和month[-2]
为undefined
您需要在日期对象中实际进行日期操作,而不仅仅是从索引中获取日期。
使用此方法获取上个月的日期
function getPrevMonth(date) {
date.setMonth(date.getMonth() - 1);
return date;
}
根据需要多次调用此方法。
<强>演示强>
function getPrevMonth(date) {
date.setMonth(date.getMonth() - 1);
return date;
}
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var cur_month = new Date();
var cur_month_now = month[cur_month.getMonth()];
var pre_month_1 = month[getPrevMonth(cur_month).getMonth()];
var pre_month_2 = month[getPrevMonth(cur_month).getMonth()];
var pre_month_3 = month[getPrevMonth(cur_month).getMonth()];
var pre_month_4 = month[getPrevMonth(cur_month).getMonth()];
var pre_month_5 = month[getPrevMonth(cur_month).getMonth()];
document.getElementById("cur_month").innerHTML = cur_month_now;
document.getElementById("pre_month_1").innerHTML = pre_month_1;
document.getElementById("pre_month_2").innerHTML = pre_month_2;
document.getElementById("pre_month_3").innerHTML = pre_month_3;
document.getElementById("pre_month_4").innerHTML = pre_month_4;
document.getElementById("pre_month_5").innerHTML = pre_month_5;
<div class="dropdown">
<button class="btn btn-danger dropdown-toggle" type="button" data-toggle="dropdown">Select Month
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li>
<a href="#" id="cur_month"></a>
</li>
<li>
<a href="#" id="pre_month_1"></a>
</li>
<li>
<a href="#" id="pre_month_2"></a>
</li>
<li>
<a href="#" id="pre_month_3"></a>
</li>
<li>
<a href="#" id="pre_month_4"></a>
</li>
<li>
<a href="#" id="pre_month_5"></a>
</li>
</ul>
</div>
答案 1 :(得分:1)
让Date
为你做环绕。我们可以对代码进行一些改进,请参阅注释:
// Array initializers are cleaner and less typing
var month = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
];
var dt = new Date();
var cur_month_now = month[dt.getMonth()];
dt.setMonth(dt.getMonth() - 1); // Date handles wrapping to previous year
var pre_month_1 = month[dt.getMonth()];
dt.setMonth(dt.getMonth() - 1);
var pre_month_2 = month[dt.getMonth()];
dt.setMonth(dt.getMonth() - 1);
var pre_month_3 = month[dt.getMonth()];
dt.setMonth(dt.getMonth() - 1);
var pre_month_4 = month[dt.getMonth()];
dt.setMonth(dt.getMonth() - 1);
var pre_month_5 = month[dt.getMonth()];
document.getElementById("cur_month").innerHTML = cur_month_now;
document.getElementById("pre_month_1").innerHTML = pre_month_1;
document.getElementById("pre_month_2").innerHTML = pre_month_2;
document.getElementById("pre_month_3").innerHTML = pre_month_3;
document.getElementById("pre_month_4").innerHTML = pre_month_4;
document.getElementById("pre_month_5").innerHTML = pre_month_5;
<div class="dropdown">
<button class="btn btn-danger dropdown-toggle" type="button" data-toggle="dropdown">Select Month
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li>
<a href="#" id="cur_month"></a>
</li>
<li>
<a href="#" id="pre_month_1"></a>
</li>
<li>
<a href="#" id="pre_month_2"></a>
</li>
<li>
<a href="#" id="pre_month_3"></a>
</li>
<li>
<a href="#" id="pre_month_4"></a>
</li>
<li>
<a href="#" id="pre_month_5"></a>
</li>
</ul>
</div>
答案 2 :(得分:0)
你可以更简洁一些,并使用模运算符:
var month = "January,February,March,April,May,June,July,August,September,October,November,December"
.split(','),
monthNum = 12 + new Date().getMonth();
Array.from(document.querySelectorAll('.dropdown-menu a'), function (elem, i) {
elem.textContent = month[(monthNum-i)%12];
});
&#13;
<div class="dropdown">
<button class="btn btn-danger dropdown-toggle" type="button" data-toggle="dropdown">Select Month
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="#" id="cur_month"></a></li>
<li><a href="#" id="pre_month_1"></a></li>
<li><a href="#" id="pre_month_2"></a></li>
<li><a href="#" id="pre_month_3"></a></li>
<li><a href="#" id="pre_month_4"></a></li>
<li><a href="#" id="pre_month_5"></a></li>
</ul>
</div>
&#13;
注意:最好使用textContent
代替innerHTML
,因为您要放置文字,而不是HTML。
答案 3 :(得分:0)
没什么新东西,但对于较小的js代码...
function getPrevMonth(date, m) {
date.setMonth(date.getMonth() - m);
return date;
}
//no need for such array
locale = "en-us";//or e.g. navigator.languages[0]
var d = new Date();
for (var i=0; i<6; i++){
//also worth trying: create element instead of writing to exiting ones only: https://www.w3schools.com/jsref/met_document_createelement.asp
document.getElementById("pre_month_"+i).innerHTML = getPrevMonth(d,1).toLocaleString(locale, { month: "long" });
}
&#13;
<div class="dropdown">
<button class="btn btn-danger dropdown-toggle" type="button" data-toggle="dropdown">Select Month
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li>
<a href="#" id="pre_month_0"></a>
</li>
<li>
<a href="#" id="pre_month_1"></a>
</li>
<li>
<a href="#" id="pre_month_2"></a>
</li>
<li>
<a href="#" id="pre_month_3"></a>
</li>
<li>
<a href="#" id="pre_month_4"></a>
</li>
<li>
<a href="#" id="pre_month_5"></a>
</li>
</ul>
</div>
&#13;