Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

JavaScript Date getMonth() Method

  • Last Updated : 12 Oct, 2021

Below is the example of Date getMonth() method. 
 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Example: 
     

javascript




<script type="text/javascript">
   // Creating a Date Object
   var DateObj = new Date('October 15, 1996 05:35:32');
 
   // Month from above Date Object is
   // Being extracted using getMonth()
   var months = DateObj.getMonth();
 
   // Printing month.
   document.write(months);
</script>
  • Output: 
     
9

The date.getMonth() method is used to fetch the month(0 to 11) from given Date object.
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.
More codes for the above method are as follows:
Program 1: Here the date of the month should lie in between 1 to 31 because no date can have 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. 
 

javascript




<script type="text/javascript">
   // Creating a Date Object
   var DateObj = new Date('October 33, 1996 05:35:32');
 
   // Month from above Date Object is being
   // Extracted using getMonth()
   var months = DateObj.getMonth();
 
   // Printing month.
   document.write(months);
</script>

Output: 
 



NaN

Program 2: If month is not given, it returns zero (0). 
 

javascript




<script type="text/javascript">
   // Creating a Date Object
   var DateObj = new Date('1996 05:35:32');
 
   // Month from above Date Object is being
   // Extracted using getMonth()
   var months = DateObj.getMonth();
 
   // Printing month.
   document.write(months);
</script>

Output: 
 

0

Program 3: If nothing as parameter is given, it returns current month. 
 

javascript




<script type="text/javascript">
   // Creating a Date Object
   var DateObj = new Date();
 
   // Month from above Date Object is being
   // Extracted using getMonth()
   var months = DateObj.getMonth();
 
   // Printing month.
  document.write(months);
</script>

Output: 
 

2

Supported Browsers: The browsers supported by JavaScript Date getMonth() method are listed below: 
 

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 3 and above
  • Safari 1 and above

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :