945,065 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 1256
  • ASP.NET RSS
Aug 15th, 2010
0

Calculate age based on DOB?

Expand Post »
Hey guys,
Im using asp.net in visual studio and im kinda new to the whole thing.
Im actually making a multi view form, and in the second view i need to calculate a persons age based on the date of birth that has been typed in view 1.Anyone know how to?I thnk its through regular expressions but we havent really been taught how to do that
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Roses89 is offline Offline
55 posts
since Apr 2010
Aug 16th, 2010
0
Re: Calculate age based on DOB?
Lets say that your DOB is being display by a textbox id = "txtDOB" then you can do this.
ASP.NET Syntax (Toggle Plain Text)
  1. int years = DateTime.Today.Year - Convert.ToDateTime(txtDOB.Text).Year;
Reputation Points: 56
Solved Threads: 56
Posting Pro in Training
jbisono is offline Offline
433 posts
since May 2009
Aug 17th, 2010
0
Re: Calculate age based on DOB?
Hmmm..So how do i get years to be displayed in view two?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Roses89 is offline Offline
55 posts
since Apr 2010
Aug 17th, 2010
0
Re: Calculate age based on DOB?
What control do you want to render the age in?
Reputation Points: 56
Solved Threads: 56
Posting Pro in Training
jbisono is offline Offline
433 posts
since May 2009
Aug 17th, 2010
0
Re: Calculate age based on DOB?
The only drawback I can see with jbisono's suggestion is that it may not be 100% accurate. As an example, my B-Day is in December 1978... if we use the formula:
ASP.NET Syntax (Toggle Plain Text)
  1. int years = DateTime.Today.Year - Convert.ToDateTime(txtDOB.Text).Year;
Then what we get is 2010 - 1978 = 32. However, at least for the next few months, I'm still 31. It is possible, however, to extend jbisono's example to include the full range of date (year, month, day) and perform a more accurate subtraction that would take the partial year into account and provide a correct age. Just requires a bit more calculation and creativity
Sponsor
Featured Poster
Reputation Points: 207
Solved Threads: 126
Practically a Posting Shark
Lusiphur is offline Offline
841 posts
since Jun 2010
Aug 17th, 2010
0
Re: Calculate age based on DOB?
You are totally right Lusiphur. Thanks for correcting me. Roses89 if you need help to complete the code let us know.
Last edited by jbisono; Aug 17th, 2010 at 2:46 pm.
Reputation Points: 56
Solved Threads: 56
Posting Pro in Training
jbisono is offline Offline
433 posts
since May 2009
Aug 18th, 2010
0
Re: Calculate age based on DOB?
If you are using calender control then use the TimeSpan property.

Get one Calender Control,textbox and Button.

ASP.NET Syntax (Toggle Plain Text)
  1. dt = Calendar1.SelectedDate
  2. ts = Date.Now - dt
  3. TextBox1.Text = ts.Days.ToString

dt is object of datetime, ts is for timespan

This code in Visual Studio Visual basic.
Reputation Points: 10
Solved Threads: 2
Junior Poster
virusisfound is offline Offline
172 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Regular expressions/url forming.
Next Thread in ASP.NET Forum Timeline: popup window in Asp.Net4 Mvc





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC