Emmanuel Akinfulubi

JavaScriptDate

getYear vs getFullYear in JavaScript

Intermediate · 3 min read

You type date.getYear(), expecting 2026, and get 126. It looks like a bug, but it is an old JavaScript method doing exactly what it was designed to do. In this tutorial you will see what getYear() returns, why you should use getFullYear() instead, and a related trap that is still very much alive: what year new Date(26, 0, 1) means.

You need to know how to make a date with new Date(year, month, day) and that months count from 0.

A bit of history

When JavaScript was young, many programs stored years as two digits: 99 for 1999. getYear() comes from that time. It returns the year minus 1900. That worked for 1999 (99), and then 2000 came out as 100.

getFullYear() was added to fix this. It returns the whole year, as you would write it.

getYear() was never removed, because removing it would break old web pages. The JavaScript standard keeps it in a special appendix, called Annex B, of features kept only for old websites. Browsers and node still have it. You should never use it in new code.

Setters

Most getters have a matching setter, a method that changes that part of the date. setFullYear(year) changes the year of an existing date in place. You will need it in Step 4.

Here is the exercise. Have a go on your own first, or read on and we will build it together.

14 Why getYear is not the year

fair

getYear() still exists in browsers. Show what it returns, and show the matching trap in the constructor: what year does new Date(99, 0, 1) mean?

Stuck? Three hints, smallest first
  1. 1. getYear returns the year minus 1900.
  2. 2. The constructor has the same 1900 offset for one- and two-digit years.
  3. 3. setFullYear is the way to reach a year in the first century.

Step 1: the two getters side by side

Step 1

const sample = new Date(2026, 0, 15);
console.log(sample.getFullYear()); // 2026
console.log(sample.getYear());     // 126
console.log(new Date(1999, 0, 1).getYear()); // 99

126 is 2026 minus 1900. For 1999, minus 1900 gives 99, which is why the method seemed fine for so long.

The answer on the page checks that getYear exists before calling it, with typeof sample.getYear === 'function'. That way the code still runs in any environment that has dropped the old method.

Step 2: the same offset in the constructor

Here is the part you can't avoid by picking a better method. When you pass a year from 0 to 99 to new Date, it adds 1900:

Step 2

console.log(new Date(99, 0, 1).getFullYear()); // 1999
console.log(new Date(26, 0, 1).getFullYear()); // 1926

If a form or a spreadsheet gives you 26 meaning 2026 and you pass it straight in, you get 1926. There is no error. Your user is just suddenly 100 years older.

Step 3: where the offset stops

The rule only applies to years 0 to 99. From 100 on, the year is taken as written:

Step 3

console.log(new Date(100, 0, 1).getFullYear());  // 100
console.log(new Date(2026, 0, 1).getFullYear()); // 2026

So four-digit years are always safe. The fix for two-digit input is to turn it into four digits yourself before it reaches the constructor, for example by adding 2000 when your data can only be from this century.

Step 4: reaching a year below 100

Now and then you really do mean a very early year, such as the year 26. The constructor can't make it, because it will add 1900. Build any date first, then set the year with setFullYear, which takes the year exactly as given:

Step 4

const yearTwentySix = new Date(2026, 0, 1);
yearTwentySix.setFullYear(26);
console.log(yearTwentySix.getFullYear()); // 26

setFullYear changes the date object itself; it doesn't make a new one. That is why the code calls it on its own line and then reads the year back.

The full solution

The panel prints both getters for the same date, the constructor's 1900 offset for 99 and 26, and the setFullYear fix. Press Run to see them.

The answer

const sample = new Date(2026, 0, 15);

// getYear is Annex B: kept alive only so old pages keep working. Guarded so
// this runs anywhere.
const legacyYear = typeof sample.getYear === 'function' ? sample.getYear() : 'not implemented';

// The same 1900 offset lives in the constructor: a year of 0 to 99 means 1900+y.
const ninetyNine = new Date(99, 0, 1);
const twentySix = new Date(26, 0, 1);

// To mean the actual year 26, set it explicitly.
const yearTwentySix = new Date(2026, 0, 1);
yearTwentySix.setFullYear(26);

console.log('getFullYear:', sample.getFullYear(), '| getYear:', legacyYear);
console.log('new Date(99, 0, 1)  ->', ninetyNine.getFullYear());
console.log('new Date(26, 0, 1)  ->', twentySix.getFullYear());
console.log('setFullYear(26)     ->', yearTwentySix.getFullYear());

Output


      
8 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 8 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

8 checks

test('getFullYear gives the year', sample.getFullYear(), 2026);
test('getYear gives the year minus 1900', legacyYear, 126);
test('so for 1999 it returned 99', new Date(1999, 0, 1).getYear(), 99);
test('the constructor reads 99 as 1999', ninetyNine.getFullYear(), 1999);
test('and 26 as 1926, not 2026', twentySix.getFullYear(), 1926);
test('only setFullYear reaches the year 26', yearTwentySix.getFullYear(), 26);
test('four-digit years are taken literally', new Date(2026, 0, 1).getFullYear(), 2026);
test('and 100 is not two-digit, so it means the year 100', new Date(100, 0, 1).getFullYear(), 100);

getYear is not gone — it is in Annex B of the specification, the appendix of things kept working for the web's sake — and it returns 126 for 2026. Never use it. getFullYear is the one that means what it says.

The interesting half is that the same 1900 offset is still in the constructor, and that one you cannot avoid by choosing a better method: new Date(26, 0, 1) is 1926. So a two-digit year arriving from a form or a CSV must be widened before it reaches the constructor, and a year genuinely below 100 has to be set with setFullYear.

Common mistakes

  • Using getYear(). It gives 126 for 2026. Always use getFullYear().
  • Passing a two-digit year to new Date. new Date(26, 0, 1) is 1926. Make the year four digits first.
  • Fixing it by adding 1900 to getYear(). It gives the right number, but it keeps an outdated method alive in your code. getFullYear() already does this.

Try it yourself

  1. Write widenYear(twoDigit) that turns 26 into 2026 and 99 into 1999, treating numbers above 50 as the 1900s.
  2. Check what new Date(0, 0, 1).getFullYear() gives. Is it the year 0?
  3. Build 1 January of the year 5 with setFullYear, and print it with toDateString().

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.