Monday, June 23, 2008

C# Get Person Age In Years By Birth Date ( DOB )

Here is quick method for C# to get the age of a person given the birth date.




public int GetAge(DateTime dob)
{
// Get year diff
int years = DateTime.Now.Year - dob.Year;
// Add year diff to birth day
dob = dob.AddYears(years);

// Subtract another year if its one day before the birth day
if (DateTime.Today.CompareTo(dob) < 0) { years--; }

return years;
}



2 comments: