JavaScript getFullYear()

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.

getFullYear() Syntax Top ↑

const year = date.getFullYear();

getFullYear() vs Deprecated getYear() Top ↑

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().

getFullYear() vs getUTCFullYear() Top ↑

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.

Calculating Age Correctly Top ↑

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;
}

Two-Digit Year Pitfalls Top ↑

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);

Common Year Mistakes Top ↑

  • Using deprecated getYear().
  • Assuming year subtraction alone gives an exact age.
  • Ignoring local/UTC boundaries around New Year.
  • Using two-digit years in numeric constructors.

getDate() continues the original date-component learning path. Date Reference




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer