为什么此switch语句不返回当前月份?

时间:2019-10-10 07:03:49

标签: javascript switch-statement

我很难理解为什么这个switch语句不返回October的值。我运行了代码段,现在是9月。

var monthOfYear = new Date().getMonth();

switch (monthOfYear) {
  case 1:
    month = 'January';
    break;    
  case 2:
    month = 'February';
    break;    
  case 3:
    month = 'March';
    break;    
  case 4:
    month = 'April';
    break;    
  case 5:
    month = 'May';
    break;    
  case 6:
    month = 'June';
    break;    
  case 7:
    month = "July";
    break;
  case 8:
    month = "August";
    break;
  case 9:
    month = "September";
    break;
  case 10:
    month = "October";
    break;
  case 11:
    month = "November";
    break;
  case 12:
    month = 'December';
    break;    
  default:
    month = "That's not a real month.";
}

outputVal.innerHTML = month;
<h3 id="outputVal"></h3>

1 个答案:

答案 0 :(得分:0)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth

  

getMonth()方法返回指定日期的月份   根据当地时间,是从零开始的值(其中零表示   一年的第一个月。