如何获得前一年的月份?

Not*_*nez 4 html javascript

我试着根据当月来得到上个月.但问题发生在"年"不是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;
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

gur*_*372 5

你得到这个未定义因为month[-1]而且month[-2]undefined

您需要在日期对象中实际执行日期操作,而不是仅从索引中获取日期.

使用此方法获取上个月的日期

function getPrevMonth(date) {
  date.setMonth(date.getMonth() - 1);
  return date;
}
Run Code Online (Sandbox Code Playgroud)

根据需要多次调用此方法.

演示

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;
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)