I need to write C# code to calculate a person's age based on a birthdate and a fixed exam visit date.
Currently the code is written like this:
public static int CalculateAge(string birthdate)
{
DateTime dob = DateTime.Parse(birthdate);
int age = DateTime.Now.Year - dob.Year;
if (DateTime.Now.Month < dob.Month)
{
age--;
}
else if (DateTime.Now.Month == dob.Month &&
DateTime.Now.Day < dob.Day)
{
age--;
}
return age;
}
I need to pass in a second variable called ExamDate to figure out the age. The way it is currently written, when the FastReport is run say 3 years down the road, obviously someone who is 22 now would be 25 when the report is displayed. I know we can't use DateTime.Now