Age in months and years, age corrected for prematurity, and age formatted for charts.
- Chronological
- 11.3 months
- Corrected
- 9.5 months
- Completed years
- 0
Installation
bunx shadcn@4.22.1 add https://organui.com/r/age.jsonThis adds lib/age.ts, with no dependencies.
Usage
import { ageInMonths, correctedAgeInMonths, formatAge } from "@/lib/age"
const birth = new Date("2025-02-10")
const measured = new Date("2026-01-20")
formatAge(ageInMonths(birth, measured)) // "11.3 months"
// Born at 32 weeks: eight weeks come off.
formatAge(correctedAgeInMonths(birth, measured, 32)) // "9.5 months"API Reference
| Export | Description |
|---|---|
ageInMonths(birth, measured) | Age in months, counting the average month of 30.4375 days as the WHO standards do. |
ageInYears(birth, on) | Completed years, as a birthday counts them, in UTC. |
correctedAgeInMonths(birth, measured, gestationalWeeks) | Age less the weeks born before 40, never below zero. Growth charts use it for babies born before 37 weeks, usually until 24 months. |
formatAge(ageMonths) | Months to one decimal under 24 months, such as "9.5 months", then years and months, such as "3 y 2 mo". |
Notes
Pass dates at UTC midnight, such as new Date("2025-02-10"), so ages don't shift with the reader's time zone.
The growth chart uses this file for its ages.