![]() |
VOOZH | about |
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).
DateObj.getMonth()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.
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.
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.
2
The browsers supported by the JavaScript Date getMonth() method are listed below: