Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to calculate birthdate based on age (day , month ,year) and current date
I tried this but the result appears incorrect

sample is

What I have tried:

Dim day, month, year As String
       day = txtday.Text
       month = txtMonth.Text
       year = txtYear.Text
       Dim intday, intmonth, intyear As Double
       intday = NumDay.Value
       intmonth = NumMonth.Value
       intyear = NumYear.Value
       intyear = Now.Year - intyear
       If Now.Day > intday Then
           intday = Now.Day - intday
       Else
           intday = 30 - intday
       End If
       If intday > 30 Then
           intday = intday - 30
           intmonth = (Now.Month + 1) - intmonth
       Else
           intmonth = Now.Month - intmonth

       End If
       Dim dateResult As New Date(CInt(intyear), CInt(intmonth), CInt(intday))

       DateTimePicker1.Value = dateResult
Posted
Updated 3-Aug-22 18:47pm
v2
Comments
Afzaal Ahmad Zeeshan 3-Aug-22 19:16pm    
What the result is, and what you expected?
mazen mougi 3-Aug-22 22:27pm    
I can get age from birth date correctly but getting birth date from age, result appear different than origin birth date
The result different in days

1 solution

Why are you using obsolete VB6 data types?
If you use the DateTime class that has been in VB.NET since 2002 it's pretty easy:
VB
Dim ageYears As Integer = 21
Dim ageMonths As Integer = 3
Dim ageDays As Integer = 22
Dim dob As DateTime = DateTime.Now.AddYears(-ageYears).AddMonths(-ageMonths).AddDays(-ageDays)
Console.WriteLine(dob)
 
Share this answer
 
Comments
Maciej Los 4-Aug-22 11:18am    
5ed!
Richard Deeming 5-Aug-22 9:54am    
It's not a VB6 type - Date is the VB.NET alias for System.DateTime, just as int is the C# alias for System.Int32. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900