I try to get the previous month according to current month. But the problem occurs when "year" is not 2017.
2 .我尽量按月领上月的货。但当“年份”不是2017年时,问题就出现了。
So how can I get the month of the previous year?. The code below will describe what I want, if anybody know how to get it please tell me the method. Thank you :)
那么我怎样才能得到上一年的月份呢?下面的代码将描述我想要的,如果有人知道如何得到它,请告诉我方法。谢谢你:)
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>
4 个解决方案
#1
5
You are getting this undefined because month[-1]
and month[-2]
are undefined
因为月[-1]和月[-2]是没有定义的
You need to actually do date manipulation in a date object rather than just getting date from index.
实际上,您需要在一个日期对象中执行日期操作,而不是从索引中获取日期。
Use this method to get last month date
使用这种方法获取上个月的日期
function getPrevMonth(date) {
date.setMonth(date.getMonth() - 1);
return date;
}
invoke this method as many times as you need.
根据需要多次调用此方法。
Demo
演示
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>
#2
1
Let Date
do the wrap-around for you. There are also a couple of improvements we can make to the code, see comments:
让约会为你做个总结。我们还可以对代码做一些改进,见注释:
// 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>
#3
0
Nothing really new, but for smaller js code …
没有什么新东西,但对于较小的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" });
}
<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>
#4
0
You could do this a bit more concise, and using modulo operator:
你可以做得更简洁一些,使用模运算符:
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];
});
<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>
NB: It is better practice to use textContent
instead of innerHTML
, since you want to put text, not HTML.
NB:最好是使用textContent而不是innerHTML,因为你想要的是文本而不是HTML。
#1
5
You are getting this undefined because month[-1]
and month[-2]
are undefined
因为月[-1]和月[-2]是没有定义的
You need to actually do date manipulation in a date object rather than just getting date from index.
实际上,您需要在一个日期对象中执行日期操作,而不是从索引中获取日期。
Use this method to get last month date
使用这种方法获取上个月的日期
function getPrevMonth(date) {
date.setMonth(date.getMonth() - 1);
return date;
}
invoke this method as many times as you need.
根据需要多次调用此方法。
Demo
演示
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>
#2
1
Let Date
do the wrap-around for you. There are also a couple of improvements we can make to the code, see comments:
让约会为你做个总结。我们还可以对代码做一些改进,见注释:
// 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>
#3
0
Nothing really new, but for smaller js code …
没有什么新东西,但对于较小的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" });
}
<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>
#4
0
You could do this a bit more concise, and using modulo operator:
你可以做得更简洁一些,使用模运算符:
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];
});
<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>
NB: It is better practice to use textContent
instead of innerHTML
, since you want to put text, not HTML.
NB:最好是使用textContent而不是innerHTML,因为你想要的是文本而不是HTML。