How to calculate age in C# using date of birth

Waqas Anwar
26 August 2014
17902 Views

Given a DateTime representing a person's birthday, you can use following code snippet to calculate the age.

DateTime birthday = new DateTime(1980, 12, 18);

DateTime today = DateTime.Today;
int age = today.Year - birthday.Year;
if (birthday > today.AddYears(-age)) age--;