Below is the example of Date setUTCFullYear() method.
// Here a date has been assigned according
// to universal time while creating Date object
'October 13, 1996 05:35:32 GMT-3:00'
// New year 1992 is being set in above Date
// Object with the help of setUTCFullYear() method
// New year from above Date Object is
// being extracted using getUTCFullYear()
B = dateobj.getUTCFullYear();
// Printing new year
The date.setUTCFullYear() method is used to set year into a date object according to universal time which is created using date() constructor.
Parameter: This method accept a single parameter as mentioned above and described below:
- year_Value: This parameter holds 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() method.
Note: The DateObj is a valid Date object created using Date() constructor in which we want to set the year.
More codes for the above method are as follows:
Program 1: If in Date() constructor we do not give any year, still setUTCFullYear() methods will be able to set new year according to universal time which is given as its parameter.
Program 2: If nothing as a parameter is given in Date() constructor, still setUTCFullYear() method will be able to set a year in the created Date object but month and date remain current ones.
Here in output 2 is the month of March because month name start from 0 to 11 i.e, 0 for January and 11for December, and 30 is the current date.
2007 2 30
- Google Chrome 1 and above
- Edge 12 and above
- Firefox 1 and above
- Internet Explorer 4 and above
- Opera 4 and above
- Safari 1 and above