I have a problem with a script which should calculate my age when I put the date of birth but I get the error NaN or that it is out of range. I declared age as int and DateNac I put [DataType(DataType.Date)], and the format in which it shows me to place the date is dd-MM-yyyy, I don't know if they could help me find the problem
function Edad() {
const fechaNacimiento = document.getElementById("fechaNac");
const edad = document.getElementById("edad");
const calcularEdad = (fechaNacimiento) => {
const fechaActual = new Date();
const anoActual = parseInt(fechaActual.getFullYear());
const mesActual = parseInt(fechaActual.getMonth()) + 1;
const diaActual = parseInt(fechaActual.getDay());
//01-01-2010
const anoNacimiento = parseInt(String(fechaNacimiento).substring(6, 10));
const mesNacimiento = parseInt(String(fechaNacimiento).substring(3, 5));
const diaNacimiento = parseInt(String(fechaNacimiento).substring(0, 2));
let edad = anoActual - anoNacimiento;
if (mesActual < mesNacimiento) {
edad--;
} else if (mesActual == mesNacimiento) {
if (diaActual < diaNacimiento) {
edad--;
}
}
return edad;
}
document.getElementById("edad").value = calcularEdad(this.value);
}
<div class="form-group col-md-4 mb-4">
<label asp-for="fechaNac" class="control-label"></label>
<input asp-for="fechaNac" class="form-control" id="fechaNac" min="1930-01-01" oninput="Edad()" />
<span asp-validation-for="fechaNac" class="text-danger"></span>
</div>
<div class="form-group col-md-2 mb-4">
<label asp-for="edad" class="control-label"></label>
<input asp-for="edad" class="form-control" id="edad" />
<span asp-validation-for="edad" class="text-danger"></span>
</div>
Your error was in the this.value that you must place in the Input to use it as it is, I leave you an example with your code
the problem was how the date was displayed, I sent the modification in case someone gets the same error