Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Date setUTCFullYear() Method

JavaScript Date setUTCFullYear() Method

The date.setUTCFullYear() method is used to set year into a date object according to universal time which is created using date() constructor. 

 Syntax:

DateObj.setUTCFullYear(year_Value);

Parameter: This method accepts a single parameter as mentioned above and described below:

  • year_Value: This parameter holds the value of year which is used to set in the date() constructor.

Return Values: It returns the new i.e. updated year according to universal time which is set by the setUTCFullYear() method. 

Note: The DateObj is a valid Date object created using the Date() constructor in which we want to set the year. More codes for the above method are as follows: 

Below are examples of the Date setUTCFullYear() method.

Example 1: 

Javascript




// Here a date has been assigned according
// to universal time while creating Date object
let 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() method
dateobj.setUTCFullYear(1992);
 
// New year from above Date Object is
// being extracted using getUTCFullYear()
let B = dateobj.getUTCFullYear();
 
// Printing new year
console.log(B);


Output:

1992

Example 2: 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. 

Javascript




// Here year according to universal time has not
// been assigned while creating Date object
let dateobj =
new Date('October 13, 05:35:32 GMT-3:00');
 
// New year 1992 is being set in above Date
// Object with the help of setUTCFullYear() method
dateobj.setUTCFullYear(1992);
 
// New year from above Date Object is
// being extracted using getUTCFullYear()
let B = dateobj.getUTCFullYear();
 
// Printing new year
console.log(B);


Output:

1992

Example 3: If nothing as a parameter is given in the Date() constructor, still the setUTCFullYear() method will be able to set a year in the created Date object but the month and date remain current ones. Here in output 2 is the month of March because the month name starts from 0 to 11 i.e., 0 for January and 11 for December, and 30 is the current date. 

Javascript




// Here nothing has been assigned
// while creating Date object
let dateobj = new Date();
 
// New year 2007 is being set in above Date
// Object with the help of setUTCFullYear() method
dateobj.setUTCFullYear(2007);
 
// Year from above Date Object is
// being extracted using getUTCFullYear()
let B = dateobj.getUTCFullYear();
 
// Month from above Date Object is
// being extracted using getUTCMonth()
let C = dateobj.getUTCMonth();
 
// Date from above Date Object is
// being extracted using getUTCDate()
let D = dateobj.getUTCDate();
 
// Printing new year
console.log(B);
 
// Printing current month
console.log(C);
 
// Printing current date
console.log(D);


Output:

2007
2
30

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 setUTCFullYear() 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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments