JavaScript | date.setMonth() function
The date.setMonth() is an inbuilt function in JavaScript which is used to set month into a date object which is created using the Date() constructor.
Syntax:
DateObj.setMonth(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 Febuary and so on till 11 for December.
Parameter: Here parameter month_Value is 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() function.
Below program illustrates the setMonth() function:
// 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 console.log(B); |
Output:
> 0
Errors and Exceptions
- Code #1: If in Date() constructor we do not give any month while creating the date object, still setMonth() function will be able to set new month in the created date object.
// Here month has not been assigned// while creating Date objectvardateobj =newDate('1996, 05:35:32');// new month of 2 is being set in above Date// Object with the help of setMonth() functiondateobj.setMonth(2);// new month from above Date Object is// being extracted using getMonth()varB = dateobj.getMonth();// Printing new monthconsole.log(B);chevron_rightfilter_noneOutput:
> 2
- Code #2: If nothing as parameter is given in Date() constructor, still setMonth() function will be able to set month but year, date etc remains current ones.
// Here nothing has been assigned// while creating Date objectvardateobj =newDate();// new month of 11 is being set in above Date// Object with the help of setMonth() functiondateobj.setMonth(11);// Month from above Date Object is// being extracted using getMonth()varB = dateobj.getMonth();// date from above Date Object is// being extracted using getDate()varC = dateobj.getDate();// year from above Date Object is// being extracted using getFullYear()varD = dateobj.getFullYear();// Printing new monthconsole.log(B);// Printing current dateconsole.log(C);// Printing current yearconsole.log(D);chevron_rightfilter_noneOutput:
> 11 > 1 > 2018
Here 11 is the new month of December, 1 is the current date and 2018 is the current year.
- Code #3: If value of month 15 is given as the parameter of setMonth() function, It will set 3 as the month because month range is form 0 to 11 and hence (15%12 = 3).
// Here date has been assigned// while creating Date objectvardateobj =newDate('October 13, 1996 05:35:32');// new month of 15 is being set in above Date// Object with the help of setMonth() functiondateobj.setMonth(15);// month from above Date Object is// being extracted using getMonth()varB = dateobj.getMonth();// year from above Date Object is// being extracted using getFullYear()varC = dateobj.getFullYear();// Printing new monthconsole.log(B);// Printing new yearconsole.log(C);chevron_rightfilter_noneOutput:
> 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.
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- JavaScript | Function Parameters
- JavaScript | Function Definitions
- JavaScript | toString( ) function
- JavaScript | Math.pow( ) Function
- JavaScript | Array every() function
- JavaScript | String() Function
- JavaScript | Function Apply
- JavaScript | Function Call
- Javascript | Number() Function
- JavaScript | Math.E() function
- JavaScript | Array some() function
- JavaScript | toFixed( ) Function
- JavaScript | Array.of() function
- JavaScript | Math.abs( ) function
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



