How to calculate age in C# using date of birth

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

Let's suppose, your date of birth is 18 December 1980

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

Now you can calculate your age using following code

DateTime today = DateTime.Today; 
int age = today.Year - birthday.Year; 

if (birthday > today.AddYears(-age)) 



About author

User avatar

Waqas Anwar

Founder of, Senior Consultant, Microsoft .NET and Web Technologies