JavaScript Date setMonth() Method
Last Updated :
19 May, 2023
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 accepts a single parameter as mentioned above and described below:
- month_Value: This parameter holds the value of the month that 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 the setMonth() method.
Note: The DateObj is a valid Date object created using the 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:
Below is an example of Date setMonth() method.
Example 1:
Javascript
let dateobj =
new Date( 'October 13, 1996 05:35:32' );
dateobj.setMonth(0);
let B = dateobj.getMonth();
console.log(B);
|
Output:
0
Example 2: If in the Date() constructor we do not give any month while creating the date object, still the setMonth() method will be able to set a new month in the created date object.
Javascript
let dateobj = new Date( '1996, 05:35:32' );
dateobj.setMonth(2);
let B = dateobj.getMonth();
console.log(B);
|
Output:
2
Example 3: If nothing as a parameter is given in the 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.
Javascript
let dateobj = new Date();
dateobj.setMonth(11);
let B = dateobj.getMonth();
let C = dateobj.getDate();
let D = dateobj.getFullYear();
console.log(B);
console.log(C);
console.log(D);
|
Output:
11
1
2018
Example 4: If the value of month 15 is given as the parameter of the setMonth() method, It will set 3 as the month because the 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, a total of 12 and we set a new month as 3 which increases year by one to 1997 from 1996 and the month becomes 3.
Javascript
let dateobj =
new Date( 'October 13, 1996 05:35:32' );
dateobj.setMonth(15);
let B = dateobj.getMonth();
let C = dateobj.getFullYear();
console.log(B);
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 setMonth() 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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...