JavaScript Date getMonth() Method
The date.getMonth() method is used to fetch the month(0 to 11) from the given Date object (0 represents the first month of the year).
Syntax:
DateObj.getMonth()
Parameter:
- This function does not accept any parameter.
Return Value:
- It returns the Month for the given Date object. The month is an integer value ranging from 0 to 11. Zero (0) means January, 1 means February, and so on till 11 means December.
Example 1: Extracting Month from a Date Object in JavaScript
The code creates a Date
object for October 15, 1996. It then uses getMonth()
to extract the month, which is zero-indexed (October corresponds to 9), and prints it to the console.
// Creating a Date Object
let DateObj = new Date('October 15, 1996 05:35:32');
// Month from above Date Object is
// Being extracted using getMonth()
let months = DateObj.getMonth();
// Printing month.
console.log(months);
Output
9
Example 2: Invalid Date Input in Date getMonth() Method
Here the date of the month should lie between 1 to 31 because no date can have a month greater than 31. That is why it returns NaN i.e, Not a Number if the month in the Date object is greater than 31.
// Creating a Date Object
let DateObj = new Date('October 33, 1996 05:35:32');
// Month from above Date Object is being
// Extracted using getMonth()
let months = DateObj.getMonth();
// Printing month.
console.log(months);
Output
NaN
Example 3: Providing no parameter in Date getMonth() Method
If nothing as a parameter is given, it returns the current month. It is returning 2 as March is the third month and months are zero-indexed in JavaScript.
// Creating a Date Object
let DateObj = new Date();
// Month from above Date Object is being
// Extracted using getMonth()
let months = DateObj.getMonth();
// Printing month.
console.log(months);
Output
2
Supported Browsers:
The browsers supported by the JavaScript Date getMonth() method are listed below:
JavaScript Date getMonth() Method- FAQs
What does the Date.prototype.getMonth() method do in JavaScript?
The getMonth() method returns the month for the specified date according to local time, as an integer between 0 and 11. January is represented by 0, February by 1, and so on up to December, which is represented by 11.
Can getMonth() be used to determine the quarter of the year?
Yes, you can use getMonth() to determine the quarter of the year by checking the returned value. For example, months 0-2 correspond to Q1, 3-5 to Q2, and so on.
Can getMonth() be used to format dates?
While getMonth() itself does not format dates, it can be used in conjunction with other methods to format dates based on the month.
Can getMonth() be used to set the month?
No, getMonth() is a getter method and cannot be used to set the month. Use setMonth() to change the month of a Date object.
What are some common use cases for getMonth()?
Common use cases include:
- Displaying the current month in a user interface.
- Performing operations based on the current month, such as seasonal calculations.
- Validating dates within a specific month.
We have a complete list of Javascript Javascript Date methods, to check those please go through the Javascript Date Object Complete Reference article.