Saturday 25 May 2013

Calculating age in C#

This is a silly one, but it's amazing how often it comes up.

so if you want to know how what's the best algorithm for calculation someone's age based on date of birth, her goes. it's in C# but it can be easily adapted.

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

No comments:

Post a Comment