format-age
is a tiny, dependency-free package for formatting age in clinical or health-related contexts. It displays age with appropriate granularity for different life stages—down to minutes for newborns and more concise formats for older individuals.
21y
– 21 years2y 2m
– 2 years, 2 months21m 29d
– 21 months, 29 days30w 4d
– 30 weeks, 4 days26d
– 26 days30mins
– 30 minutes
format-age
is available as an npm package can be installed with one of the following commands:
npm
npm install @avenue3-dev/format-age
pnpm
pnpm add @avenue3-dev/format-age
// Birth date as a Date object
formatAge(new Date('2003-05-09'));
// → '21y'
// Birth date ISO 8601 string input
formatAge('2022-03-01');
// → '2y 2m'
// Calculating relative age or age of death
formatAge(new Date('2002-03-01'), new Date('2022-03-01'));
// → '20y'
Check out our changelog for key highlights, performance improvements, new features, and notable bugfixes.
Licensed under the MIT License.