Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is getMonth() in JavaScript?

Umme Ammara

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The getMonth() method in JavaScript returns the month for the specified date according to the local time.

Syntax

We declare the getMonth() method as shown below:

Parameters and return value

The getMonth() method does not take any parameters. Instead, it returns the numeric value (0 - 11) corresponding to the month for the specified date. For example, 0 corresponds to January, and 11 corresponds to December.

Examples

The following code shows the use of the getMonth() method in JavaScript.

First, we declare a new Date object with the current date and time. We then use the getMonth() method to get the month of the specified date:

//Date Object
var d = new Date();
//getMonth() Method
var m = d.getMonth();
//Display the year
console.log("Month:", m);

In the example below, we declare the new Date object with a specified date. The getMonth() method returns the corresponding month for this specified date, as shown below:

//Date Object with a specified date
var d = new Date("December 25, 1999 15:15:00");
//getMonth() Method
var m = d.getMonth();
//Display
console.log("Month:", m);

RELATED TAGS

javascript

CONTRIBUTOR

Umme Ammara
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring