Open In App

JavaScript Date setUTCMonth() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax:  

dateObj.setUTCMonth(month_Value);

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

  • month_Value This parameter holds the value of the month which is used to set in the date() constructor.

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

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

Below are examples of the Date setUTCMonth() method. 

Example 1:

Javascript




// Here a date has been assigned according to
// universal time while creating Date object
let 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()
let B = dateobj.getMonth();
 
// Printing new month
console.log(B);


Output: 

0

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

Javascript




// Here month has not been assigned according to
// universal time while creating Date object
let 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()
let B = dateobj.getUTCMonth();
 
// Printing new month
console.log(B);


Output: 

2

Example 3: If nothing as a parameter is given in the 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




// Here nothing has been assigned according to
// universal time while creating Date object
let 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()
let B = dateobj.getUTCMonth();
 
// Date from above Date Object is
// being extracted using getUTCDate()
let C = dateobj.getUTCDate();
 
// Year from above Date Object is
// being extracted using getUTCFullYear()
let D = dateobj.getUTCFullYear();
 
// Printing new month
console.log(B);
 
// Printing current date
console.log(C);
 
// Printing current year
console.log(D);


Output: 

11
1
2018

Example 4: If the value of month 15 is given as the parameter of the setUTCMonth() method, It will set 3 as the month because the month range is from 0 to 11, and hence, 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, a total of 12 and we set the new month as 3 which increases year by one to 1997 from 1996 and the month becomes 3 according to universal time. 

Javascript




// Here date has been assigned according to
// universal time while creating Date object
let 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()
let B = dateobj.getUTCMonth();
 
// Year from above Date Object is
// being extracted using getUTCFullYear()
let C = dateobj.getUTCFullYear();
 
// Printing new month
console.log(B + '<br />');
 
// Printing new year
console.log(C);


Output: 

3
1997

We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.

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

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

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Last Updated : 22 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads