JavaScript | date.setUTCFullYear() function
The date.setUTCFullYear() is an inbuilt function in JavaScript which is used to set year into a date object according to universal time which is created using date() constructor.
Syntax:
DateObj.setUTCFullYear(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 Values: It returns the new i.e updated year according to universal time which is set by setUTCFullYear() function.
Below program illustrates the setUTCFullYear() function:
<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 year 1992 is being set in above Date // Object with the help of setUTCFullYear() function dateobj.setUTCFullYear(1992); // new year from above Date Object is // being extracted using getUTCFullYear() var B = dateobj.getUTCFullYear(); // Printing new year document.write(B); </script> |
Output:
1992
Errors and Exceptions
- Example 1: If in Date() constructor we do not give any year, still setUTCFullYear() function will be able to set new year according to universal time which is given as its parameter.
<script>// Here year according to universal time has not// been assigned while creating Date objectvardateobj =newDate('October 13, 05:35:32 GMT-3:00');// new year 1992 is being set in above Date// Object with the help of setUTCFullYear() functiondateobj.setUTCFullYear(1992);// new year from above Date Object is// being extracted using getUTCFullYear()varB = dateobj.getUTCFullYear();// Printing new yeardocument.write(B);</script>chevron_rightfilter_noneOutput:
1992
- Example 2: If nothing as parameter is given in Date() constructor, still setUTCFullYear() function will be able to set year in the created Date object but month and date remains current ones.
<script>// Here nothing has been assigned// while creating Date objectvardateobj =newDate();// new year 2007 is being set in above Date// Object with the help of setUTCFullYear() functiondateobj.setUTCFullYear(2007);// Year from above Date Object is// being extracted using getUTCFullYear()varB = dateobj.getUTCFullYear();// month from above Date Object is// being extracted using getUTCMonth()varC = dateobj.getUTCMonth();// date from above Date Object is// being extracted using getUTCDate()varD = dateobj.getUTCDate();// Printing new yeardocument.write(B +"<br>");// Printing current monthdocument.write(C +"<br>");// Printing current datedocument.write(D);</script>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. - Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
- How to call a function that return another function in JavaScript ?
- JavaScript | Function Invocation
- JavaScript | String() Function
- JavaScript | Symbol.for() function
- JavaScript | Math.E() function
- JavaScript | toString( ) function
- JavaScript | Array every() function
- JavaScript | Array.of() function
- JavaScript | Function binding
- JavaScript | toPrecision( ) Function
- JavaScript | Array some() function
- What is the inline function in JavaScript ?
- Javascript | Number() Function
- JavaScript | Function Apply
- JavaScript | Function Definitions
Supported Browsers: The browsers supported by JavaScript date.setUTCFullYear() Function are listed below:
Recommended Posts:
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.


