The getFullYear() method is a built-in function in JavaScript that is used to retrieve the current year as a four-digit number. This method can be called on a Date object and returns the current year at the time the function is executed.
dateObject.getFullYear()
In this syntax, dateObject
is an instance of the Date
object on which the getFullYear()
method is called.
getFullYear() JavaScript example
Simple example code.
const today = new Date();
const year = today.getFullYear();
console.log(year);
Output:
Here’s a practical example of how you might use the getFullYear()
method in JavaScript:
// Calculate someone's age based on their birth year
function calculateAge(birthYear) {
const currentYear = new Date().getFullYear();
const age = currentYear - birthYear;
return age;
}
// Example usage:
const birthYear = 1990;
const age = calculateAge(birthYear);
console.log(`You are ${age} years old.`);
Do comment if you have any doubts or suggestions on this JS date object topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version