Below is the example of Date getFullYear() method.
- Example:
<script>// Here a date has been assigned// while creating Date objectvardateobj =newDate('October 15, 1996 05:35:32');// year from above date object is// being fetched using getFullYear().varB = dateobj.getFullYear();// Printing yeardocument.write(B);</script>chevron_rightfilter_none - Output:
1996
The 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.
More codes for the above method are as follows:
Program 1: If nothing as parameter is given to the Date() constructor, then the getFullYear() function returns current year.
<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
Program 2: 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 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 document.write(B); </script> |
Output:
NaN
Program 3: It has applications such as getting current year.It is used in websites to validate the age of the user.
<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() method are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- JavaScript Date now() Method
- JavaScript Date UTC() Method
- JavaScript Date getMonth() Method
- JavaScript Date toDateString() Method
- JavaScript Date getUTCDate() Method
- JavaScript Date getHours() Method
- JavaScript Date getMinutes() Method
- JavaScript Date setMilliseconds() Method
- JavaScript Date getDate() Method
- JavaScript Date getSeconds() Method
- JavaScript Date getDay() Method
- JavaScript Date getTime() Method
- JavaScript Date toLocaleTimeString() Method
- JavaScript Date getMilliseconds() Method
- JavaScript Date toLocaleString() Method
- JavaScript Date setDate() Method
- JavaScript Date getUTCMonth() Method
- JavaScript Date getUTCMinutes() Method
- JavaScript Date setFullYear() Method
- JavaScript Date setHours() Method
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


