getFullYear()

ES1+

Returns the year of the specified date according to local time.

Syntax

date.getFullYear()

Return Value

number

A number representing the year

Examples

JavaScript
const date = new Date('2024-01-15');
console.log(date.getFullYear());
Output:
// 2024

📌 When to Use

Use getFullYear() for copyright notices, age calculations, year-based filtering, or any display requiring the full 4-digit year.

⚠️ Common Mistakes

Using the deprecated getYear() method instead of getFullYear() - getYear() returns years since 1900.

Forgetting that getFullYear() uses local time, which may differ from the server time zone.

✅ Best Practices

Always use getFullYear() instead of getYear() for Y2K-safe code.

For UTC year, use getUTCFullYear() to avoid timezone-related issues.

⚡ Performance Notes

getFullYear() is a simple property extraction with O(1) complexity. There is no performance concern even when called frequently.

🌍 Real World Example

Age Calculator

Calculate exact age from birth date, accounting for whether birthday has occurred this year.

function calculateAge(birthDate) {
  const today = new Date();
  const birth = new Date(birthDate);

  let age = today.getFullYear() - birth.getFullYear();
  const monthDiff = today.getMonth() - birth.getMonth();

  // Adjust if birthday hasn't occurred yet this year
  if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
    age--;
  }

  return {
    age,
    birthYear: birth.getFullYear(),
    nextBirthday: `${birth.getMonth() + 1}/${birth.getDate()}`
  };
}

console.log(calculateAge('1990-06-15'));
// { age: 33, birthYear: 1990, nextBirthday: '6/15' }

Related Methods