Age

static public int Age(this DateTime dateOfBirth) {
     if (DateTime.Today.Month < dateOfBirth.Month ||
	 DateTime.Today.Month == dateOfBirth.Month &&
	  DateTime.Today.Day < dateOfBirth.Day) {
          return DateTime.Today.Year - dateOfBirth.Year - 1;
	} else
	 return DateTime.Today.Year - dateOfBirth.Year;
}
Example:
DateTime henrybirthdate = new DateTime(1998,10,12);

int age = henrybirthdate.Age();

Description

Get the actual age of a person

Details

Double click on the code to select all.

 

;