fix(growth): show age in years+months format

- Show age as "1y 4mo" instead of just months
- Add formatAge helper function
- Apply to Latest Reading and WHO card

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Manohar Gupta 2026-05-17 14:57:51 +05:30
parent 38e18c12f3
commit a3d0b06501

View file

@ -44,6 +44,23 @@ interface Goal {
targetDate?: string; targetDate?: string;
} }
function formatAge(birthDate: string, measurementDate?: string): string {
const birth = new Date(birthDate);
const now = measurementDate ? new Date(measurementDate) : new Date();
const years = Math.floor((now.getTime() - birth.getTime()) / (1000 * 60 * 60 * 24 * 365));
const months = Math.floor(((now.getTime() - birth.getTime()) % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24 * 30));
if (years > 0 && months > 0) {
return `${years}y ${months}mo`;
} else if (years > 0) {
return `${years}y`;
} else if (months > 0) {
return `${months}mo`;
} else {
return "Newborn";
}
}
export default function GrowthPage() { export default function GrowthPage() {
const { childId, child, familyId } = useFamily(); const { childId, child, familyId } = useFamily();
const { theme } = useTheme(); const { theme } = useTheme();
@ -415,7 +432,9 @@ export default function GrowthPage() {
<div className="flex justify-between items-center mb-3"> <div className="flex justify-between items-center mb-3">
<div> <div>
<div className="font-semibold text-rose-600 dark:text-rose-300">Latest Reading</div> <div className="font-semibold text-rose-600 dark:text-rose-300">Latest Reading</div>
<div className="text-sm text-gray-500">{new Date(latest.measured_at).toLocaleDateString()}</div> <div className="text-sm text-gray-500">
{new Date(latest.measured_at).toLocaleDateString()} ({child ? formatAge(child.birthDate, latest.measured_at) : ""})
</div>
</div> </div>
{velocity && ( {velocity && (
<div className={`text-sm font-medium ${velocity.direction === "up" ? "text-green-500" : "text-amber-500"}`}> <div className={`text-sm font-medium ${velocity.direction === "up" ? "text-green-500" : "text-amber-500"}`}>
@ -480,8 +499,8 @@ export default function GrowthPage() {
className="w-full p-4 flex justify-between items-center hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors" className="w-full p-4 flex justify-between items-center hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors"
> >
<div className="text-left"> <div className="text-left">
<div className="font-semibold text-lg">{child.name} - WHO Standards</div> <div className="font-semibold text-lg">{child.name}</div>
<div className="text-sm text-gray-500">{ageMonths} months old</div> <div className="text-sm text-gray-500">{formatAge(child.birthDate)}</div>
</div> </div>
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
{savedGoals.weightKg && ( {savedGoals.weightKg && (