Back to Blog
3 min read

How to Calculate Age in TypeScript (Date Object and Day.js Examples)

Learn how to calculate age from date of birth in TypeScript with typed helpers, a plain Date solution, and a Day.js example you can copy-paste.

How to Calculate Age in TypeScript (Date Object and Day.js Examples)

How to Calculate Age in TypeScript (Date Object and Day.js Examples)

If you need to calculate age in TypeScript, the main job is the same as in JavaScript: compare the reference date with the date of birth and reduce the year count if the birthday has not happened yet.

Quick option: use an age calculator first

If you want to verify exact results in years, months, and days, use the age calculator first, then match your code output to it.

Age in completed years with TypeScript

This is the most common definition of age for forms, apps, and eligibility checks.

function ageYears(dob: Date, asOf: Date = new Date()): number {
  let years = asOf.getFullYear() - dob.getFullYear();
  const monthDiff = asOf.getMonth() - dob.getMonth();

  if (monthDiff < 0 || (monthDiff === 0 && asOf.getDate() < dob.getDate())) {
    years--;
  }

  return years;
}

const dob = new Date(1998, 9, 12); // 12 Oct 1998
console.log(ageYears(dob));

Parse YYYY-MM-DD safely

A common bug comes from passing new Date('1998-10-12'), because string parsing can introduce UTC and time zone surprises. A small parser is safer.

function parseLocalDate(value: string): Date {
  const [year, month, day] = value.split('-').map(Number);
  return new Date(year, month - 1, day);
}

const dob = parseLocalDate('1998-10-12');
console.log(ageYears(dob));

Typed helper for string inputs

If your app accepts dates as strings, wrap parsing and calculation into one typed function.

function ageFromDobString(dob: string, asOf: Date = new Date()): number {
  const birthDate = parseLocalDate(dob);
  return ageYears(birthDate, asOf);
}

Using Day.js in TypeScript

If you already use Day.js, it can keep parsing and comparisons cleaner.

import dayjs from 'dayjs';

function ageYearsDayjs(dob: string, asOf: string = dayjs().format('YYYY-MM-DD')): number {
  const birth = dayjs(dob);
  const ref = dayjs(asOf);
  let years = ref.year() - birth.year();
  const hadBirthday = ref.month() > birth.month() || (ref.month() === birth.month() && ref.date() >= birth.date());
  if (!hadBirthday) years--;
  return years;
}

Common mistakes

  • Subtracting years only and ignoring whether the birthday has already happened this year.
  • Parsing date strings without controlling the format.
  • Testing only normal dates and skipping leap years or month boundaries.

Related

FAQ

What is the correct definition of age in TypeScript apps?

Usually it means completed years as of today or another reference date. That is what the examples above calculate.

Can I calculate exact age in years, months, and days too?

Yes, but it needs more careful date math. For a quick check, compare your output with the online calculator first.