JavaScript | date.getFullYear() Function
The date.getFullYear() is an inbuilt function in JavaScript which is used to fetch the year from a given Date object.
Syntax:
DateObj.getFullYear()
In the above syntax, DateObj is a valid Date object created using Date() constructor from which we want to fetch the year.
Parameter: This function does not take any parameters. It is just used along with a Date Object from which we want to fetch year.
Return Values: It returns the year for the given date.
The following program illustrate the getFullYear() method:
Example 1:
<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 document.write(B); </script> |
Output:
1996
Errors and Exceptions:
- Example 1: 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.
<script>// Here a date has been assigned// while creating Date objectvardateobj =newDate('October 45, 1996 05:35:32');// year from above date object is// being fetched using getFullYear().varB = dateobj.getFullYear();// Printing yeardocument.write(B);</script>chevron_rightfilter_noneOutput:
NaN
- Example 2: If nothing as parameter is given to the Date() constructor, then the getFullYear() function returns current year.
<script>// Creating Date Objectvardateobj =newDate();// year from above object// is being fetched using getFullYear().varB = dateobj.getFullYear();// Printing current yeardocument.write(B);</script>chevron_rightfilter_noneOutput:
2018
Application: It has applications such as getting current year.It is used in websites to validate the age of the user. The following program shows one of the applications of this function. It gives the current year.
Example 1:
<script> // Creating Date Object var dateobj = new Date(); // year from above object // is being fetched using getFullYear() var B = dateobj.getFullYear(); // Printing current year document.write(B); </script> |
Output:
2018
Supported Browsers: The browsers supported by JavaScript date.getFullYear() Function are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- JavaScript | Function Invocation
- JavaScript | String() Function
- JavaScript | Symbol.for() function
- JavaScript | Math.E() function
- JavaScript | toString( ) function
- JavaScript | Array every() function
- JavaScript | Array.of() function
- JavaScript | Function binding
- JavaScript | toPrecision( ) Function
- JavaScript | Array some() function
- What is the inline function in JavaScript ?
- Javascript | Number() Function
- JavaScript | Function Apply
- JavaScript | Function Definitions
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.
Improved By : RahulRanjan4


