diff --git a/src/lib/utils.ts b/src/lib/utils.ts index a047bab..052ee1e 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -24,3 +24,16 @@ export function getNombreProfesional(user?: Usuario | null): string { if (nombre) return nombre; return 'Admin'; } + +export function calcularEdad(fechaNacimiento?: string): string | number { + if (!fechaNacimiento) return '-'; + const hoy = new Date(); + const cumple = new Date(fechaNacimiento); + if (isNaN(cumple.getTime())) return '-'; + let edad = hoy.getFullYear() - cumple.getFullYear(); + const m = hoy.getMonth() - cumple.getMonth(); + if (m < 0 || (m === 0 && hoy.getDate() < cumple.getDate())) { + edad--; + } + return edad; +} diff --git a/src/sections/EditIngreso.tsx b/src/sections/EditIngreso.tsx index a33583d..cb00dd0 100644 --- a/src/sections/EditIngreso.tsx +++ b/src/sections/EditIngreso.tsx @@ -8,7 +8,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@ import { Badge } from '@/components/ui/badge'; import { toast } from 'sonner'; import type { Paciente, Cama, Area, Internacion } from '@/types'; -import { getNombreProfesional } from '@/lib/utils'; +import { getNombreProfesional, calcularEdad } from '@/lib/utils'; import { useHospitalStore } from '@/hooks/useHospitalStore'; interface EditIngresoProps {