Wednesday, May 2, 2012

Decent Age Algorithm for C# for string birthdate


When you have a birthdate in a string format, such as "3/17/1980":
strBirthdate = "3/17/1980" //common with HTML forms
DateTime birthdate = DateTime.Parse(strBirthdate);
int age = DateTime.Today.Year - birthdate.Year;
if (birthdate > DateTime.Today.AddYears(-age)) age--;