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.

Member Avatar for nicentral

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

Member Avatar for nicentral

Actually I just thought of an easier way.

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

This should give you the age.

Andy

Member Avatar for nicentral

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

Actually it should be:

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

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

Member Avatar for bescobar

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.