String? calcularEdadDesdeControl(
String? birthDateStr,
String? controlDate,
) {
/// MODIFY CODE ONLY BELOW THIS LINE
String calcularEdadEntreFechas(String birthDateStr, DateTime? checkupDate) {
try {
// Validar si checkupDate es null
if (checkupDate == null) return 'Fecha no seleccionada';
// Parsear la fecha de nacimiento desde String d/M/y
List<String> parts = birthDateStr.split('/');
if (parts.length != 3) return 'Fecha nacimiento inválida';
int day = int.parse(parts[0]);
int month = int.parse(parts[1]);
int year = int.parse(parts[2]);
DateTime birthDate = DateTime(year, month, day);
// Calcular diferencias
int years = checkupDate.year - birthDate.year;
int months = checkupDate.month - birthDate.month;
int days = checkupDate.day - birthDate.day;
// Ajustar días y meses si son negativos
if (days < 0) {
months--;
int prevMonth = checkupDate.month - 1;
int yearOfPrevMonth = checkupDate.year;
if (prevMonth == 0) {
prevMonth = 12;
yearOfPrevMonth--;
}
int daysInPrevMonth = DateTime(yearOfPrevMonth, prevMonth + 1, 0).day;
days += daysInPrevMonth;
}
if (months < 0) {
years--;
months += 12;
}
// Armar resultado
List<String> resultParts = [];
if (years > 0) resultParts.add('$years ${years == 1 ? "año" : "años"}');
if (months > 0)
resultParts.add('$months ${months == 1 ? "mes" : "meses"}');
if (days > 0) resultParts.add('$days ${days == 1 ? "día" : "días"}');
if (resultParts.isEmpty) return 'Recién nacido';
return resultParts.join(', ');
} catch (e) {
return 'Fecha inválida';
}
}
/// MODIFY CODE ONLY ABOVE THIS LINE
}
formula para calcular edad entre fecha de nacimiento y otra fecha dada no funciona el test de la custom function, da null
Custom Code
probe cambiando los argumentos a datetime o string. probe varias otras formulas y todas dan null en el test. y en la pagina de la app no trae ningun valor a pesar de que estan los argumentos. cual podria ser el error? gracias y saludos!
No