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


#332369    Author: Deepika Haridas    24 Dec 2008 Member Level: Gold   Points : 6


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);
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);

Thanks & Regards,
Deepika - Sr. Editor
Webmaster, GujaratSpider
"Experience is the name everyone gives to his mistakes"

#332423    Author: Pradeep M  25 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;

This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.