-1

I'm having trouble understanding why this switch statement isn't returning the value of October. I run the snippet and it's returning September.

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>
Millhorn
  • 2,363
  • 6
  • 34
  • 64

1 Answers1

0

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

The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).

Alien
  • 3,620
  • 1
  • 15
  • 32