JavaScript .getMonth()

christian.dinh's avatar
Published Jun 22, 2021Updated Jun 5, 2023
Contribute to Docs

Called from an instance of the Date class, will return the month of the year.

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

Syntax

myDate.getMonth();

The return value of getMonth() will only be an integer number between 0 and 11, meaning the month of the year:

  • 0 represents January
  • 1 represents February
  • 2 represents March
  • 10 represents November
  • 11 represents December

Example

To determine if it is June or not:

const today = new Date('June 25, 2021 13:41:30');
if (today.getMonth() === 5) {
console.log('Time flies, it is now June.');
} else {
console.log('It is not 10 in the morning now.');
}
// Output: Time flies, it is now June.

Codebyte Example

The example below defines a new Date object dateTimeNow, an array months containing the names of the calendar months, and stores the result of the getMonth() method in a new variable month. Finally, the current date\time, the current month, and the months remaining in the current year are logged to the console.

Code
Output

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours