How to calculate birthdate based on age (day , month ,year) and current date I tried this but the result appears incorrect I can get age from birth date correctly but getting birth date from age, result appear different than origin birth date sample when calculate age for 15/4/2022 and then calculate birth date for this age it appears 12/4/2022
Dim day, month, year As Integer
day = CInt(NumDay.Value)
month = CInt(NumMonth.Value)
year = CInt(NumYear.Value)
Dim dateResult As Date = Now
DateTimePicker1.Value = dateResult.AddYears(-year).AddMonths(-month).AddDays(-day)
enter code here
Function AgeCalculator(ByVal FromDate As Date, Todate As Date, ByRef year As Integer, ByRef month As Integer, ByRef day As Integer, Optional flgyearOnly As Boolean = False) As String '23/11/2017
If Not IsDate(FromDate) Then Return ""
Dim tmpYear As String = "", tmpMonth As String = ""
Dim tmpdiffDaya As Integer = 0
Dim tmpAge As String = ""
tmpdiffDaya = CInt(DateDiff(DateInterval.Day, (FromDate), Todate))
If tmpdiffDaya <= 0 Then Return ""
If tmpdiffDaya > 0 And tmpdiffDaya <= 29 Then
Return tmpdiffDaya & " Days"
End If
If tmpdiffDaya = 30 Then
Return " 1 Month"
End If
tmpYear = CStr(tmpdiffDaya / 365)
If InStr(tmpYear, ".") > 0 Then
tmpYear = Microsoft.VisualBasic.Left(tmpYear, InStr(tmpYear, ".") - 1)
End If
year = CInt(tmpYear)
If Val(tmpYear) = 0 Then tmpYear = ""
If Val(tmpYear) > 0 Then
tmpAge = tmpYear & " years"
If flgyearOnly Then Return tmpAge
End If
tmpdiffDaya = CInt((tmpdiffDaya - (Val(tmpYear) * 365)))
tmpMonth = CStr(tmpdiffDaya / 30)
If InStr(tmpMonth, ".") > 0 Then
tmpMonth = Microsoft.VisualBasic.Left(tmpMonth, InStr(tmpMonth, ".") - 1)
End If
month = CInt(tmpMonth)
If Val(tmpMonth) = 0 Then
tmpMonth = ""
Else
If CInt(tmpMonth) > 0 Then
tmpAge &= " " & tmpMonth & " Months"
End If
End If
tmpdiffDaya = CInt((tmpdiffDaya - (Val(tmpMonth) * 30)))
day = tmpdiffDaya
If Val(tmpdiffDaya) > 0 Then
tmpAge &= " " & tmpdiffDaya & " Days"
End If
Return tmpAge
End Function
CodePudding user response:
You need to call AddDays
first, then AddMonths
, then AddYears
. If you wanted to calculate a person's age in years, months and days then you would calculate the years first, then the months, then the days. If you're doing the calculation the other way, you have to actually do it the other way. If you subtract years first then a leap year could throw the calculation out. If you subtract months before days then that calculation will be affected by the different numbers of days in each month.
Private Function GetPastDate(years As Integer, months As Integer, days As Integer) As Date
Return Date.Today.AddDays(-days).AddMonths(-months).AddYears(-years)
End Function
My date of birth is June 19, 1969, which means that I am 53 years, 1 month and 16 days old today (August 4, 2022). If I use the method above with those numbers then I get the correct date of birth. If I call those methods the opposite way around, I get June 18 instead of 19.
EDIT:
I haven't looked closely but it appears that your AgeCalculator
doesn't work. I just tested the following code:
Module Module1
Sub Main()
Dim dateOfBirth = #4/15/2012#
Dim years As Integer
Dim months As Integer
Dim days As Integer
CalculateAge(dateOfBirth, years, months, days)
Dim output = GetPastDate(years, months, days)
End Sub
Private Sub CalculateAge(dateOfBirth As Date, ByRef years As Integer, ByRef months As Integer, ByRef days As Integer)
Dim temp As Date
Dim currentDate = Date.Today
dateOfBirth = dateOfBirth.Date
years = 0
months = 0
Do
temp = dateOfBirth.AddYears(1)
If temp > currentDate Then
Exit Do
End If
years = 1
dateOfBirth = temp
Loop
Do
temp = dateOfBirth.AddMonths(1)
If temp > currentDate Then
Exit Do
End If
months = 1
dateOfBirth = temp
Loop
days = (currentDate - dateOfBirth).Days
End Sub
Private Function GetPastDate(years As Integer, months As Integer, days As Integer) As Date
Return Date.Today.AddDays(-days).AddMonths(-months).AddYears(-years)
End Function
End Module
and it worked as expected. The values of years
, months
and days
were 10, 3 and 20 respectively and the value of output
was #4/15/2012#
, the same as the initial input.