Forums » .NET » ASP.NET »

how to calculate age from date of birth in c#

Posted Date: 24 Dec 2008      Posted By:: mukul prady     Member Level: Silver    Member Rank: 3806     Points: 1   Responses: 2

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      Member Level: Gold      Member Rank: 6     Date: 24/Dec/2008   Rating: 4 out of 54 out of 54 out of 54 out of 5     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      Member Level: Silver      Member Rank: 1435     Date: 25/Dec/2008   Rating: 4 out of 54 out of 54 out of 54 out of 5     Points: 6

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

Post Reply

 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.

Return to Discussion Forum
Start new thread

Subscribe to Subscribers
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John

Online Members

Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India