Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Date getFullYear() Method

Improve Article
Save Article
  • Last Updated : 29 Nov, 2022
Improve Article
Save Article

The JavaScript Date getFullYear() Method is used to fetch the year from a given Date object.

Syntax: 

DateObj.getFullYear()

Parameter: This function does not accept any parameters.

Return Values: It returns the year for the given date.

Below is an example of the Date.getFullYear() method. 

Example 1: 

javascript




<script>
    // Here a date has been assigned
    // while creating Date object
    var dateobj = new Date('October 15, 1996 05:35:32');
     
    // year from above date object is
    // being fetched using getFullYear().
    var B = dateobj.getFullYear();
     
    // Printing year
     console.log(B);
</script>

Output: 

1996

More codes for the above method are as follows: 

Example 2: If nothing as a parameter is given to the Date() constructor, then the getFullYear() function returns the current year. 

javascript




<script>
    // Creating Date Object
    var dateobj = new Date();
     
    // year from above object
    // is being fetched using getFullYear().
    var B = dateobj.getFullYear();
     
    // Printing current year
     console.log(B);
</script>

Output: 

2022

Example 3: The date of the month must lie between 1 to 31 because a month cannot have more than 31 days. That is why it returns NaN i.e, not a number because the date for the month does not exist. Hence, Year will not exist when the date of the month is set as 45 or any number which does not lie between 1 to 31.

javascript




<script>
    // Here a date has been assigned
    // while creating Date object
    var dateobj = new Date('October 45, 1996 05:35:32');
     
    // year from above date object is
    // being fetched using getFullYear().
    var B = dateobj.getFullYear();
     
    // Printing year
    console.log(B);
</script>

Output: 

NaN

Example 4: It has applications such as getting the current year. It is used on websites to validate the age of the user. 

javascript




<script>
    // Creating Date Object
    var dateobj = new Date();
     
    // Year from above object
    // is being fetched using getFullYear()
    var B = dateobj.getFullYear();
     
    // Printing current year
    console.log(B);
</script>

Output: 

2022

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 JavaScript Date.getFullYear() 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

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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!