Three date bugs in a 10-line age calculator (and the TZ= trick that exposes them)
Age calculators look like a beginner exercise. Subtract the years, subtract the months, subtract the days, borrow when something goes negative.
Mine had all of that. It still told a user born on 31 January that on 1 March they were 26 years, 1 month and −2 days old. On their birthday it said their next birthday was 365 days away.
Here are the three bugs and how I fixed them. If you've ever written new Date(input.value), at least one of them is probably in your code too.
Bug 1: new Date("2026-05-15") is not 15 May in your time zone
An <input type="date"> gives you a string like "2026-05-15". Passing that to new Date() creates midnight UTC, not local midnight. That's ECMAScript's rule for date-only ISO strings.
Then the code calls getDate(), getMonth() and so on, which read local time. In Pakistan (UTC+5) that UTC midnight is 05:00 on the same day, so the date looks right. But:
const nb = new Date(b.getFullYear(), a.getMonth(), a.getDate()); // local midnight
if (nb < b) nb.setFullYear(b.getFullYear() + 1);
On the birthday, nb is 00:00 local and b is 05:00 local, so nb < b is true and the next birthday jumps a year ahead. Hence "365 days".
In US time zones it's worse. UTC midnight is still the previous day locally, so every result was off by a day. The same inputs gave "1 month −2 days" in Lahore and "0 months 29 days" in Los Angeles.
Fix: parse the string yourself as a local calendar date.
function pd(v) {
const [y, m, d] = v.split('-').map(Number);
return new Date(y, m - 1, d); // local midnight
}
The same trap applies the other way. input.valueAsDate = new Date() sets the field to today in UTC, which shows yesterday's date for anyone ahead of UTC just after midnight. Build the YYYY-MM-DD string from the local getFullYear/getMonth/getDate instead.
Bug 2: borrowing days from the wrong month
The classic algorithm:
let d = b.getDate() - a.getDate();
if (d < 0) {
mo--;
d += new Date(b.getFullYear(), b.getMonth(), 0).getDate(); // days in previous month
}
Born 31 Jan, today is 1 March: d = 1 - 31 = -30. Borrow February's 28 days and you get −2. The borrow assumes the previous month is long enough to absorb the difference, and February isn't.
Bug 3: there's no single "right" way to borrow, so define one
What is 31 January + 1 month? There's no 31 February. You have to choose, and the common choice (it's what most date libraries do) is to clamp to the last day of the month: 31 Jan + 1 month = 28 Feb, or 29 in a leap year.
Once you've chosen, the cleanest algorithm doesn't borrow at all. Count whole months, then count the leftover days:
function addMonths(t, n) {
const y = t.getFullYear(), m = t.getMonth() + n;
const last = new Date(y, m + 1, 0).getDate(); // last day of target month
return new Date(y, m, Math.min(t.getDate(), last)); // clamp
}
function dayDiff(x, y) { // DST-safe
return Math.round((Date.UTC(y.getFullYear(), y.getMonth(), y.getDate()) - Date.UTC(x.getFullYear(), x.getMonth(), x.getDate())) / 864e5);
}
function age(a, b) {
let months = (b.getFullYear() - a.getFullYear()) * 12 + (b.getMonth() - a.getMonth());
if (addMonths(a, months) > b) months--; // this month's anniversary not reached
const days = dayDiff(addMonths(a, months), b);
return { years: Math.floor(months / 12), months: months % 12, days };
}
Days can never be negative now, because they're measured forward from the last whole-month anniversary. 31 Jan → 1 Mar is 1 month (to 28 Feb) plus 1 day.
The next birthday falls out of the same helper: addMonths(dob, 12 * yearsSince). That also handles 29 February birthdays (celebrated on the 28th in non-leap years).
How I tested it
- Edge cases: month ends, 29 Feb in leap and non-leap years, the birthday itself, 31 Dec → 1 Jan.
- Three time zones: each case ran under
TZ=Asia/Karachi,TZ=America/Los_AngelesandTZ=UTCin Node. All three now print identical results. The old code gave a different answer in each zone I tried. - 20,000 random date pairs: asserting that days are never negative and months stay within 0-11.
The TZ= trick is the takeaway. If your date code gives different answers when you change the time zone of the machine running it, you have a bug, even if it looks fine where you live.
The calculator is part of 14FreeTools, a set of free tools that run entirely in the browser. People mostly use it to check job and admission age limits "as on" a closing date, where being one day off actually matters.
Comments
No comments yet. Start the discussion.