help pls i need someone that can help me when i enter the date of birth using the datetimepicker box it automatically compute the age and output in a textbox.text

Dani AI

Generated

To compute age correctly and update it automatically when the user changes the DateTimePicker, avoid parsing strings or subtracting only the year. Use DateTime arithmetic and adjust for whether the birthday has occurred this year. The pattern age = today.Year - dob.Year followed by If today < dob.AddYears(age) Then age -= 1 handles leap years (Feb 29) and all edge cases because it relies on DateTime.AddYears rather than month counts.

' In your form:
Private Sub Form1_Load(...) Handles MyBase.Load
    DateTimePicker1.MaxDate = Date.Today   ' Prevent future DOBs.
    AddHandler DateTimePicker1.ValueChanged, AddressOf UpdateAge
    UpdateAge()
End Sub

Private Sub UpdateAge()
    Dim dob As Date = DateTimePicker1.Value.Date
    Dim today As Date = Date.Today
    Dim age As Integer = today.Year - dob.Year
    If today < dob.AddYears(age) Then age -= 1
    TextBox2.Text = age.ToString()
End Sub

A few tips: use Date.Today for date-only comparisons (no time component), not Now (docs vs docs). Let the DateTimePicker give you a Date directly instead of splitting strings; it is culture-safe and avoids parsing bugs. If you prefer not to wire up AddHandler, you can still respond to the control’s ValueChanged event and call UpdateAge() from there.

Recommended Answers

All 8 Replies

Dim myCoolBirthday As Date = DateTimePicker1.Value.Date '// selected value from DateTimePicker.
        Dim currentDate As Date = Today '// value of current date.
        Dim myAge As Integer = currentDate.Year - myCoolBirthday.Year '// subtract years from current date.
        MsgBox("My age: " & myAge & vbNewLine & "Just don't tell anyone. :)") '// display age.

here is my code....
code for button..
Dim born, today As Integer
Dim now() As String
Dim now1 As String
now1 = Date.Today
now = now1.Split("/")
born = CInt(TextBox1.Text)
today = CInt(now(2))
born = today - born
TextBox2.Text = born

what i want is that i want to use the datetimepicker and it automatically compute the age in another textbox without clicking the button..

owww thank you again hehehe :)

i change the code to textbox2.text = myAge but the problem is if i change the year in datetimepicker it does not compute anymore the age

Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
        Dim Age As Double = Math.Floor(DateDiff(DateInterval.Month, DateTimePicker1.Value, System.DateTime.Now) / 12)
        TextBox1.Text = "Your age is: " & Age
    End Sub

solved hehehe:)

here is my code....
code for button..
Dim born, today As Integer
Dim now() As String
Dim now1 As String
now1 = Date.Today
now = now1.Split("/")
born = CInt(TextBox1.Text)
today = CInt(now(2))
born = today - born
TextBox2.Text = born

what i want is that i want to use the datetimepicker and it automatically compute the age in another textbox without clicking the button..

I have modified your code slightly, but for future references arjen, please refrain from using Variables that relate to vb.net.
Example, you declared now and today, which are both used by vb.net to access certain values.

TextBox1.Text = DateTimePicker1.Value.Year
        Dim now1 As String = Date.Today
        Dim now() As String = now1.Split("/")
        Dim today As Integer = CInt(now(2))
        Dim born As Integer = Today - CInt(TextBox1.Text)
        TextBox2.Text = born

Textbox1.text=now.year - datetimepicker1.value.year

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.