JavaScript Date setUTCFullYear() Method

Below is the example of Date setUTCFullYear() method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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() method
       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>

    chevron_right

    
    

  • Output:
    1992

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 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
   // Here year according to universal time has not
   // been assigned while creating Date object
   var 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()
   var B = dateobj.getUTCFullYear();
  
   // Printing new year
   document.write(B);
</script>

chevron_right


Output:

1992

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
   // Here nothing has been assigned
   // while creating Date object
   var 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()
   var B = dateobj.getUTCFullYear();
  
  // Month from above Date Object is
  // being extracted using getUTCMonth()
  var C = dateobj.getUTCMonth();
  
  // Date from above Date Object is
  // being extracted using getUTCDate()
  var D = dateobj.getUTCDate();
  
  // Printing new year
  document.write(B + "<br />");
  
  // Printing current month
  document.write(C + "<br />");
  
  // Printing current date
  document.write(D);
</script>

chevron_right


Output:

2007
2
30

Supported Browsers: The browsers supported by JavaScript Date setUTCFullYear() Method are listed below:

  • Google Chrome
  • Internet Explorer
  • Mozilla Firefox
  • Opera
  • Safari

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.