JavaScript | date.getMonth() method

The date.getMonth() is an inbuilt function in JavaScript which is used to fetch the month from given Date object.

Syntax:

DateObj.getMonth()

In the above syntax, DateObj is a valid Date object created using Date() conctructor from which we want to fetch months.

Parameter: This function does not takes any parameter. It is just used along with a Date Object from which we want to fetch Months.

Return Value: It returns the Month for the given Date object. Month is an integer value ranging from 0 to 11. Zero (0) means January, 1 means February and so on till 11 means December.



Below program illustrate the date.getMonth() method:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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.
console.log(months);
  
</script>

chevron_right


Output:

9

Errors and Exceptions:

  • Program 1: Here date of the month should must 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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.
    console.log(months);
      
    </script>

    chevron_right

    
    

    Output:

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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.
    console.log(months);
      
    </script>

    chevron_right

    
    

    Output:

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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.
    console.log(months);
      
    </script>

    chevron_right

    
    

    Output:

    2
  • Application: It has many applications such as getting current month. Below program shows one of the applications of this function. It gives the current month.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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.
    console.log(months);
      
    </script>

    chevron_right

    
    

    Output:

    2


    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.