JavaScript Date setMonth() Method

Below is the example of Date setMonth() method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
       // Here a date has been assigned
       // while creating Date object
       var dateobj = 
       new Date('October 13, 1996 05:35:32');
      
       // new month of January is being set in above Date
       // Object with the help of setMonth() function
       dateobj.setMonth(0);
      
       // new month from above Date Object is
       // being extracted using getMonth()
       var B = dateobj.getMonth();
      
       // Printing new month
       document.write(B);
    </script>

    chevron_right

    
    

  • Output:
    0

The date.setMonth() method is used to set month into a date object which is created using the Date() constructor.

Syntax:

DateObj.setMonth(month_Value);

Parameter: This method accept a single parameter as mentioned above and described below:

  • month_Value: This parameter holds the value of month which we want to set in the date created using the Date() constructor.

Return Values: It returns the new i.e updated month which is set by setMonth() method.



Note: The DateObj is a valid Date object created using Date() constructor in which we want to set the month.The value of the month is from 0 to 11 because the total number of months in a year is 12 from January to December. Value 0 is used for January, 1 for February, and so on till 11 for December.

More codes for the above method are as follows:

Program 1: If in Date() constructor we do not give any month while creating the date object, still setMonth() method will be able to set new month in the created date object.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
   // Here month has not been assigned
   // while creating Date object
   var dateobj = new Date('1996, 05:35:32');
  
   // New month of 2 is being set in above Date
   // Object with the help of setMonth() method
   dateobj.setMonth(2);
  
   // New month from above Date Object is
   // being extracted using getMonth()
   var B = dateobj.getMonth();
  
   // Printing new month
   document.write(B);
</script>

chevron_right


Output:

2

Program 2: If nothing as parameter is given in Date() constructor, still setMonth() method will be able to set month but year, date etc remains current ones. Here 11 is the new month of December, 1 is the current date and 2018 is the current year.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
   // Here nothing has been assigned
   // while creating Date object
   var dateobj = new Date();
  
   // New month of 11 is being set in above Date
   // Object with the help of setMonth() method
   dateobj.setMonth(11);
  
   // Month from above Date Object is
   // being extracted using getMonth()
   var B = dateobj.getMonth();
  
   // Date from above Date Object is
   // being extracted using getDate()
   var C = dateobj.getDate();
  
   // Year from above Date Object is
   // being extracted using getFullYear()
   var D = dateobj.getFullYear();
  
   // Printing new month
   document.write(B + "<br />");
  
   // Printing current date
   document.write(C + "<br />");
  
   // Printing current year
   document.write(D);
</script>

chevron_right


Output:

11
1
2018

Program 3: If value of month 15 is given as the parameter of setMonth() method, It will set 3 as the month because month range is from 0 to 11 and hence (15%12 = 3).
Here 3 is the new month of April and the year becomes 1997 from 1996 because the month range is from 0 to 11 i.e, total 12 and we set a new month as 3 which increases year by one to 1997 from 1996 and the month becomes 3.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
   // Here date has been assigned
   // while creating Date object
   var dateobj = 
   new Date('October 13, 1996 05:35:32');
  
   // new month of 15 is being set in above Date
   // Object with the help of setMonth() function
   dateobj.setMonth(15);
  
   // Month from above Date Object is
   // being extracted using getMonth()
   var B = dateobj.getMonth();
  
   // Year from above Date Object is
   // being extracted using getFullYear()
   var C = dateobj.getFullYear();
  
   // Printing new month
   document.write(B + "<br />");
  
   // Printing new year
   document.write(C);
</script>

chevron_right


Output:

3
1997

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

  • Google Chrome
  • Internet Explorer
  • Mozilla Firefox
  • Opera
  • Safari

full-stack-img




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.



Improved By : nidhi_biet