Date.prototype.getFullYear()

Return the four-digit year of a date according to local time.

Signature
getFullYear()
  • Added in JavaScript ES1

Return value

numberAn integer representing the year of the given date according to local time — a four-digit number for years between 1000 and 9999, for example 1995. Returns NaN if the date is invalid.

Errors

Throws a TypeError if called on a value that is not a Date object.

Changelog

  • PHP ES1Introduced as the replacement for the legacy getYear(), which returns year minus 1900 and survives only in Annex B.

Returns the year as an absolute number, in the timezone the code is running in. It is the safe replacement for the deprecated getYear(), which returns the year minus 1900 and therefore reports 2024 as 124.

When to reach for it

Use it for anything that reasons about the calendar year rather than an instant: copyright lines, age calculations, grouping records by year, checking whether two dates fall in the same year, or building a year dropdown. Pair it with getMonth() and getDate() when you need to rebuild a date from its parts.

What trips people up

The result is local. Near midnight on 31 December the local year and the UTC year disagree, so a report generated at 23:30 in Tokyo and one generated simultaneously in London can be filed under different years. Use getUTCFullYear() when the answer must be timezone-independent.

Age arithmetic is the classic bug. Subtracting the two years alone overstates the age for anyone whose birthday has not yet come round this year — you must also compare the month, and the day when the months are equal.

On an invalid date it returns NaN, and NaN propagates silently through any arithmetic that follows.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Print a copyright year that updates itself
  • Compute someone's age from a date of birth
  • Group log entries or invoices into yearly buckets
  • Test whether a year is a leap year before rendering a February calendar

Frequently asked questions

What is the difference between getFullYear() and getYear()?

getFullYear() returns the absolute year such as 2024, while the deprecated getYear() returns the year minus 1900, so 2024 comes back as 124. getYear() exists only in Annex B for legacy compatibility and should never be used in new code.

How do I get the current year in JavaScript?

Call new Date().getFullYear(), which returns the year in the user's local timezone. Use getUTCFullYear() instead if the value must be the same for every user.

Why does getFullYear() return NaN?

The Date is invalid — usually because an unparsable string was passed to the constructor. Check with Number.isNaN(date.getTime()) before reading any component.

More examples

Age, calculated correctly

Year subtraction alone gives 34 in all three cases. The month-and-day correction turns the first into 33, which is the answer people expect.

Example · javascript
function ageOn(birth, reference) {
  let age = reference.getFullYear() - birth.getFullYear();
  const monthDiff = reference.getMonth() - birth.getMonth();
  if (monthDiff < 0 || (monthDiff === 0 && reference.getDate() < birth.getDate())) {
    age--;
  }
  return age;
}

const dob = new Date(1990, 4, 15); // 15 May 1990
console.log(ageOn(dob, new Date(2024, 1, 1)));  // before the birthday
console.log(ageOn(dob, new Date(2024, 4, 15))); // on the birthday
console.log(ageOn(dob, new Date(2024, 8, 1)));  // after it

Leap year test

Both report false, true, false, true. 1900 is not a leap year and 2000 is — the century rule that hand-rolled y % 4 checks get wrong.

Example · javascript
// Ask the calendar rather than implementing the rule
function isLeapYear(year) {
  return new Date(year, 1, 29).getDate() === 29;
}

console.log([2023, 2024, 1900, 2000].map(isLeapYear));

// The same answer from the arithmetic rule
const rule = (y) => (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0;
console.log([2023, 2024, 1900, 2000].map(rule));

Local year versus UTC year

getUTCFullYear() is 2025 everywhere. getFullYear() reports 2024 for anyone running more than 30 minutes behind UTC, which is how New Year reports land in the wrong bucket.

Example · javascript
// A moment just after UTC midnight on New Year's Eve
const turn = new Date(Date.UTC(2025, 0, 1, 0, 30));

console.log(turn.toISOString());
console.log(turn.getUTCFullYear());          // always 2025
console.log(turn.getFullYear());             // depends on the timezone
console.log(turn.getFullYear() === turn.getUTCFullYear());

Discussion

  • Be the first to comment on this lesson.