Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript Date setFullYear() Method

  • Last Updated : 26 Oct, 2021

Below is the example of Date setFullYear() method.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Example:




    <script>
       // 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() method
       dateobj.setFullYear(1992);
      
       // New year from above Date Object is
       // being extracted using getFullYear()
       var B = dateobj.getFullYear();
      
       // Printing new year
       document.write(B);
    </script>
  • Output:
    1992

The date.setFullYear() method is used to set year into a date object which is created using Date() constructor.

Syntax:

DateObj.setFullYear(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 Value: It returns the new date with updated year which is set by setFullYear() 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 setFullYear() method set new year which is given as its parameter.




<script>
   // Here year has not been assigned
   // while creating Date object
   var dateobj = 
   new Date('October 13, 05:35:32');
  
   // new year 1992 is being set in above Date
   // Object with the help of setFullYear() method
   dateobj.setFullYear(1992);
  
   // New year from above Date Object is
   // being extracted using getFullYear()
   var B = dateobj.getFullYear();
  
   // Printing new year
   document.write(B);
</script>

Output:

1992

Program 2: If nothing as parameter is given in Date() constructor, still setFullYear() method set year but month and date become 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 11 for December and 30 is the current date.




<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 setFullYear() method
   dateobj.setFullYear(2007);
  
   // Year from above Date Object is
   // being extracted using getFullYear()
   var B = dateobj.getFullYear();
  
   // Month from above Date Object is
   // being extracted using getMonth()
   var C = dateobj.getMonth();
  
   // Date from above Date Object is
   // being extracted using getDate()
   var D = dateobj.getDate();
  
   // Printing new year
   document.write(B + "<"<br />">");
  
   // Printing current month
   document.write(C + "<"<br />">");
  
   // Printing current date
   document.write(D);
</script>

Output:

2007
2
30

Supported Browsers: The browsers supported by JavaScript Date setFullYear() method are listed below:

  • 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



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!