954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

calculate age

hi..
can u help me calculate age using vb.net.
i have txtage.text and txtdob.text.
the format dob is 21.03.1978.

and if txtage.text <35 it will in group 1, if txtage.text < 49 it will in group 2. the group in radiobutton. i've bee thinking this problem almost 3 days. please anyone out there help me.

sara
Newbie Poster
10 posts since Apr 2005
Reputation Points: 11
Solved Threads: 0
 

Check out the books online entries for the DateDiff function and DatePart funciton.

The DateDiff will give you the number of years in between, but you'll need to use some logic to determine if the current year should be counted because it's not a whole year.

Does this make sense?

Andy

nicentral
Junior Poster
166 posts since Apr 2005
Reputation Points: 14
Solved Threads: 5
 

Actually I just thought of an easier way.

intAge = DateDiff(DateInterval.Month, dteDateOne, dteDateTwo) / 12

This should give you the age.

Andy

nicentral
Junior Poster
166 posts since Apr 2005
Reputation Points: 14
Solved Threads: 5
 
intAge = DateDiff(DateInterval.Month, dteDateOne, dteDateTwo) / 12

Of course you would need to change my variable names to yours, and also you may need to cast your strings from the text boxes to date data types.

Andy

nicentral
Junior Poster
166 posts since Apr 2005
Reputation Points: 14
Solved Threads: 5
 

Actually it should be:

intAge = Math.Floor(DateDiff(DateInterval.Month, dteDateOne, dteDateTwo) / 12)

logantuk
Newbie Poster
1 post since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Dim BornDate As Date = #6/18/1982#

Dim Interval As TimeSpan = Now - BornDate

Dim years As Integer = CInt(Interval.TotalDays / 365)

If BornDate.AddYears(years) <= Now Then
MsgBox(years)
Else
MsgBox(years - 1)
End If

snahc
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

try this code :

Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
TextBox5.Text = DateTimePicker1.Value
TextBox9.Text = CInt(DateDiff(DateInterval.Day, CDate(TextBox5.Text), CDate(Now)) / 365)
End Sub

bescobar
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You