I need to calculate the age of a person from their date of birth, but the calculation is doing me wrong, I give you an example.
Pepito was born on December 17, 1997, at this time Pepito is 19 years old, but the calculation shows me as if he were 20 years old.
My code is the following:
//FECHA NACIMIENTO EN LA BASE DE DATOS
[Display(Name = "Fecha Nacimiento")]
[DataType(DataType.Date)]
public DateTime fecha_nacimiento { get; set; }
//CALCULO DE LA EDAD
[NotMapped]
[Display(Name = "Edad")]
public int edad { get { return DateTime.Now.Year - fecha_nacimiento.Year; } }
In my database the date is like this:
1997-12-17 00:00:00,000
It is required to carry out a validation to know the current age, first the difference in years is obtained, then it is compared against the current date and if it is less it means that it has not turned years, then a year is subtracted to obtain the age as of the date of birth. today:
Here you can see theDemostración
In your case, your code would look like this:
I leave you a small fix on the code of the previous partner. I hope it helps you.
All the best.