Use getFullYear() to get the year from a JavaScript Date in local time. It returns the full year such as 2026.
const date = new Date();
console.log(date.getFullYear());
Older JavaScript has a getYear() method, but it is deprecated and should not be used for new code.
const year = date.getFullYear();
const fullYear = date.getFullYear();
// Avoid: date.getYear()
getYear() historically returns the year minus 1900 for common modern dates, which makes it error-prone. Use getFullYear().
const localYear = date.getFullYear();
const utcYear = date.getUTCFullYear();
These values can differ around New Year when local time and UTC are in different calendar years.
Subtracting years alone can be off by one before the birthday occurs. Compare month and day as well.
function getAge(birthDate, today = new Date()) {
let age = today.getFullYear() - birthDate.getFullYear();
const monthDiff = today.getMonth() - birthDate.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
Avoid two-digit years in numeric Date construction because JavaScript has historical handling for values from 0 to 99. Use a four-digit year.
const clear = new Date(2026, 8, 10);
getYear().getDate() continues the original date-component learning path. Date Reference
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.