String dob = "2010/01/01";//read from your database to get dob, just an example DateTime dateOfBirth = DateTime.Parse( dob ); Int32 age = DateTime.Now.Year - dateOfBirth.Year; //age=1 in above code
public static int GetAge(string strDate) { DateTime birthDate = DateTime.Parse(strDate); DateTime n = DateTime.Now; int age = DateTime.Now.Year - birthDate.Year; if (n.Month < birthDate.Month || (n.Month == birthDate.Month && n.Day < birthDate.Day)) age--; return age; }
public static class ExtensionMethods { public static int Age(this DateTime dt) { TimeSpan span = DateTime.Now.Date - dt.Date; return Math.Min(1, span.TotalDays % 365); } }
DateDiff
// ---------------------------------------------------------------------- public int Age( DateTime dob ) { return new DateDiff( dob ).ElapsedYears; } // Age
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)