Age code often passes its tests on the developer's machine and gives a wrong answer a few time zones away. The first two causes below can be reproduced in Node by changing the TZ environment variable. The third, setMonth, gives the same answer in every zone.
Today depends on where you are
At 23:30 UTC on 31 December 2026 it is still 31 December in London and Los Angeles, and already 1 January 2027 in Auckland. Someone born on 1 January 2000 is 26 in the first two places and 27 in the third. A server that takes "today" from its own clock gives everyone its own answer.
Ask for the date in a named zone and keep plain numbers:
function dateIn(timeZone, instant = new Date()) { const parts = new Intl.DateTimeFormat('en-GB', { timeZone, year: 'numeric', month: 'numeric', day: 'numeric', }).formatToParts(instant); const n = (type) => Number(parts.find((p) => p.type === type).value); return { y: n('year'), m: n('month'), d: n('day') }; } In a browser, Intl.DateTimeFormat().resolvedOptions().timeZone supplies the device's zone.
A date-only string shifts the birthday in some zones
A function that compares local getters of new Date('2000-01-01') reports 27 in Los Angeles at noon on 31 December 2026, a day early, and 26 in London. The string is midnight UTC, so those getters already show 31 December 1999 in Los Angeles. Adding T00:00:00 makes a date-time form, which MDN says is read as local time, and the getters then stay on 1 January. That only helps later comparisons if they use the same local clock. Split the string and keep numbers instead:
function parseIso(iso) { const [y, m, d] = iso.split('-').map(Number); return { y, m, d }; } function wholeYears(born, on) { const beforeBirthday = on.m < born.m || (on.m === born.m && on.d < born.d); return on.y - born.y - (beforeBirthday ? 1 : 0); } setMonth overflows
new Date(2026, 0, 31) followed by setMonth(1) gives 3 March 2026. February has 28 days that year, so the 31st spills into March. Ask how long the target month is instead. Day 0 in Date.UTC is the last day of the previous month, with no local-time effects:
function daysInMonth(y, m) { return new Date(Date.UTC(y, m, 0)).getUTCDate(); } function anniversary(born, n) { const t = born.m - 1 + n; const y = born.y + Math.floor(t / 12); const m = ((t % 12) + 12) % 12 + 1; if (born.d <= daysInMonth(y, m)) return { y, m, d: born.d }; return m === 12 ? { y: y + 1, m: 1, d: 1 } : { y, m: m + 1, d: 1 }; } A missing day moves to the 1st of the next month, and every anniversary is counted from the birth date. Born on 31 May 1992, one month on is 1 July and two months on is 31 July.
Tests that do not care about the zone
Pass fixed instants and plain dates, and the same test file passes in every zone:
import assert from 'node:assert/strict'; const instant = new Date('2026-12-31T23:30:00Z'); const born = parseIso('2000-01-01'); assert.equal(wholeYears(born, dateIn('Europe/London', instant)), 26); assert.equal(wholeYears(born, dateIn('Pacific/Auckland', instant)), 27); Running it under TZ=UTC, TZ=America/Los_Angeles and TZ=Pacific/Auckland shows whether anything reads the machine's zone. agecalculator.com takes today's date from the zone the device reports, shows it, and lets you change it. wholeYears and anniversary agree with the site's own date code on about 190,000 random date pairs.