Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript Date setUTCMonth() Method

  • Last Updated : 26 Oct, 2021

Below is the example of Date setUTCMonth() 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>
  // 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() method
  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>                       

Output: 

0

The date.setUTCMonth() method is used to set month according to universal time into a date object which are created using date() constructor. 

Syntax:  



dateObj.setUTCMonth(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 is used to set in date() constructor.

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

Note: The dateObj is a valid Date object created using Date() constructor in which we want to set the month.

More codes for the above method are as follows:

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

Javascript




<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() method
  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>

Output: 

2

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

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

Javascript




<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() method
  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>                       

Output: 

11
1
2018

Program 3: If value of month 15 is given as the parameter of setUTCMonth() method, 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. 
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 new month as 3 which increases year by one to 1997 from 1996 and the month becomes 3 according to universal time. 

Javascript




<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() method
  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>                       

Output: 

3
1997

Supported Browsers: The browsers supported by JavaScript Date setUTCMonth() Method are listed below: 

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

 




My Personal Notes arrow_drop_up

Start Your Coding Journey Now!