/** * Age ↔ birth-date helpers. The A4 form collects a whole-year **age** (per the wireframe) * while the contract stores a `birthDate` (`YYYY-MM-DD`) — we map between them here. Birth * date is approximated as 1 January of the birth year; that round-trips back to the same age. */ /** Approximate ISO birth date (`YYYY-MM-01-01`) for a whole-year age. */ export function ageToBirthDate(age: number, now: Date = new Date()): string { const year = now.getUTCFullYear() - Math.max(0, Math.floor(age)); return `${year}-01-01`; } /** Whole-year age from an ISO birth date (floored); null for an empty/invalid date. */ export function birthDateToAge(birthDate: string | null | undefined, now: Date = new Date()): number | null { if (!birthDate) return null; const date = new Date(birthDate); if (Number.isNaN(date.getTime())) return null; let age = now.getUTCFullYear() - date.getUTCFullYear(); const monthDelta = now.getUTCMonth() - date.getUTCMonth(); if (monthDelta < 0 || (monthDelta === 0 && now.getUTCDate() < date.getUTCDate())) age -= 1; return age < 0 ? null : age; }