You must Sign In to post a response.

how to calculate age from date of birth in c#

i want to calculate age from date of birth

suppose the date of birth of some one is 10/06/1984. so today he is 23 years old.
so i want to print his age as 23
how to do this


Comments

Author: Deepika Haridas24 Dec 2008 Member Level: Gold   Points : 6

Hi,

private void button3_Click(object sender, System.EventArgs e)
{
System.DateTime date2 = System.DateTime.Now;
System.DateTime date1 = new DateTime(1980,07,06);
long yrs;
yrs = DateDiff(date1,date2);
MessageBox.Show(yrs.ToString());
}
public long DateDiff(System.DateTime StartDate,System.DateTime EndDate)
{
long lngDateDiffValue = 0;
System.TimeSpan TS = new System.TimeSpan(EndDate.Ticks - StartDate.Ticks);
lngDateDiffValue = (long)( TS.Days / 365);
return (lngDateDiffValue);
}

Author: Pradeep M25 Dec 2008 Member Level: Silver   Points : 6

public void age()
{
DateTime dtDOB = new DateTime(1984, 10, 6);
TimeSpan ts = DateTime.Now.Subtract(dtDOB);
int years = ts.Days / 365;
Response.Write(years.ToString());
}