JavaScript | date.setFullYear() Function
The date.setFullYear() is an inbuilt function in JavaScript which is used to set year into a date object which is created using Date() constructor.
Syntax:
DateObj.setFullYear(year_Value)
DateObj is a valid Date object created using Date() constructor in which we want to set the year.
Parameter: Here parameter year_Value is the value of year which is used to set in Date() constructor.
Return Value: It returns the new date with updated year which is set by setFullYear() function.
Below program illustrates the setFullYear() function:
// Here a date has been assigned // while creating Date object var dateobj = new Date('October 13, 1996 05:35:32'); // new year 1992 is being set in above Date // Object with the help of setFullYear() function dateobj.setFullYear(1992); // new year from above Date Object is // being extracted using getFullYear() var B = dateobj.getFullYear(); // Printing new year console.log(B); |
Output:
1992
Errors and Exceptions
- Code #1: If in Date() constructor we do not give any year, still setFullYear() function set new year which is given as its parameter.
// Here year has not been assigned// while creating Date objectvardateobj =newDate('October 13, 05:35:32');// new year 1992 is being set in above Date// Object with the help of setFullYear() functiondateobj.setFullYear(1992);// new year from above Date Object is// being extracted using getFullYear()varB = dateobj.getFullYear();// Printing new yearconsole.log(B);chevron_rightfilter_noneOutput:
1992
- Code #2: If nothing as parameter is given in Date() constructor, still setFullYear() function set year but month and date become current ones.
// Here nothing has been assigned// while creating Date objectvardateobj =newDate();// new year 2007 is being set in above Date// Object with the help of setFullYear() functiondateobj.setFullYear(2007);// Year from above Date Object is// being extracted using getFullYear()varB = dateobj.getFullYear();// month from above Date Object is// being extracted using getMonth()varC = dateobj.getMonth();// date from above Date Object is// being extracted using getDate()varD = dateobj.getDate();// Printing new yearconsole.log(B);// Printing current monthconsole.log(C);// Printing current dateconsole.log(D);chevron_rightfilter_noneOutput:
2007 2 30
Here in output 2 is the month of March because month name start from 0 to 11 i.e, 0 for January and 11 for December.
And 30 is the current date.
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.



