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.

Dani AI

Generated

A reliable, easy-to-read approach is to parse the DOB string with a strict format (the OP uses dd.MM.yyyy), then compute the age as the difference in years and subtract one if the birthday this year has not yet happened. That method is simple, handles leap years correctly, and avoids the off-by-one errors you get when you approximate by dividing days or months. Several earlier replies touched on alternatives: recommended DateDiff (which can work), noted you must floor results, and / showed day-based division — those work with extra checks but are more error-prone than the year-minus-year + month/day check below.

Imports System.Globalization

Dim dob As DateTime
If DateTime.TryParseExact(txtDob.Text.Trim(), New String() {"dd.MM.yyyy", "d.M.yyyy"}, CultureInfo.InvariantCulture, DateTimeStyles.None, dob) Then
    Dim today As DateTime = DateTime.Today
    Dim age As Integer = today.Year - dob.Year
    If (today.Month < dob.Month) OrElse (today.Month = dob.Month AndAlso today.Day < dob.Day) Then
        age -= 1
    End If
    txtAge.Text = age.ToString()
    If age < 35 Then
        RadioButtonGroup1.Checked = True
    ElseIf age < 49 Then
        RadioButtonGroup2.Checked = True
    Else
        RadioButtonGroup3.Checked = True
    End If
Else
    ' invalid input: clear/notify as appropriate
    txtAge.Text = String.Empty
End If

Notes and troubleshooting: always Trim() input and use TryParseExact to avoid culture-dependent parsing. Decide how to handle the boundary values (e.g., age == 35 goes to group 2 with the < 35 rule). If DOB entry formats vary, add the acceptable format strings to the TryParseExact array. Avoid dividing total days by 365 unless you also validate with an AddYears check — the explicit year/month/day comparison shown here is clearer and safer.

Recommended Answers

All 6 Replies

Member Avatar for Member #33065

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 Member #33065

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 Member #33065

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 Member #930300

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.