JavaScript | date.setUTCMonth()

The date.setUTCMonth() is an inbuilt function in JavaScript which is used to set month according to universal time into a date object which are created using date() constructor.
Syntax:

dateObj.setUTCMonth(month_Value);

DateObj is a valid Date object created using Date() constructor in which we want to set the month. Value of 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.
Parameter: Here parameter is month_Value i.e, the value of month which is used to set in date() constructor.
Return values: It returns the new i.e updated month which is set by setUTCMonth() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



0

Errors and Exceptions

  • Example 1: If in Date() constructor we do not give any month, still setUTCMonth() function set new month which is given as its parameter.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
      // Here month has not been assigned according to
      // universal time while creating Date object
      var dateobj = new Date('1996, 05:35:32 GMT-3:00');
      
      // new month of 2 is being set in above Date
      // Object with the help of setUTCMonth() function
      dateobj.setUTCMonth(2);
      
      // new month from above Date Object is
      // being extracted using getUTCMonth()
      var B = dateobj.getUTCMonth();
      
      // Printing new month
      document.write(B);
      
    </script>

    chevron_right

    
    

    Output:

    2
  • Example 2: If nothing as parameter is given in Date() constructor, still setUTCMonth() function set month but year, date etc become current ones according to universal time.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
      // Here nothing has been assigned according to
      // universal time while creating Date object
      var dateobj = new Date();
      
      // new month of 11 is being set in above Date
      // Object with the help of setUTCMonth() function
      dateobj.setUTCMonth(11);
      
      // Month from above Date Object is
      // being extracted using getUTCMonth()
      var B = dateobj.getUTCMonth();
      
      // date from above Date Object is
      // being extracted using getUTCDate()
      var C = dateobj.getUTCDate();
      
      // year from above Date Object is
      // being extracted using getUTCFullYear()
      var D = dateobj.getUTCFullYear();
      
      // Printing new month
      document.write(B + '<br>');
      
      // Printing current date
      document.write(C + '<br>');
      
      // Printing current year
      document.write(D + '<br>');
                              
    </script>                        

    chevron_right

    
    

    Output:

    11
    1
    2018

    Here 11 is the new month of December, 1 is the current date and 2018 is the current year according to universal time.

  • Example 3: If value of month 15 is given as the parameter of setUTCMonth() function, It will set 3 as the month because month range is from 0 to 11 and hence 15-12=3, here 12 is subtracted because 0 to 11 is 12.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
      // Here date has been assigned according to
      // universal time while creating Date object
      var dateobj = new Date('October 13, 1996 05:35:32 GMT-3:00');
      
      // new month of 15 is being set in above Date
      // Object with the help of setUTCMonth() function
      dateobj.setUTCMonth(15);
      
      // month from above Date Object is
      // being extracted using getUTCMonth()
      var B = dateobj.getUTCMonth();
      
      // year from above Date Object is
      // being extracted using getUTCFullYear()
      var C = dateobj.getUTCFullYear();
      
      // Printing new month
      document.write(B + '<br>');
      
      // Printing new year
      document.write(C);
                             
    </script>                        

    chevron_right

    
    

    Output:

     3
    1997
    

    Here 3 is the new month of April and year becomes 1997 from 1996 because month range is form 0 to 11 i.e, total 12 and we set new month as 3 which increase year by one to 1997 from 1996 and month becomes 3 according to universal time.

  • Supported Browsers: The browsers supported by JavaScript date.setUTCMonth() Function are listed below:

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


    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.




    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.