Reputation: 27
net coding and i am really stuck in this basic age calculation. I have created a form field and in a groupbox it should display the age of a person. The biggest problem i am facing with the code is factoring in the month. I have tried all types such as using DateInterval.Month but still no luck. Here MyEntPatient.DOB is the DOB of patient
Dim Years As Integer
Dim BDAY As New DateTime(Now.Year)
BDAY = MyEntPatient.DOB
If (BDAY > Now) Then
Years = DateDiff(DateInterval.Year, MyEntPatient.DOB, Now) - 1
Else
Years = DateDiff(DateInterval.Year, MyEntPatient.DOB, Now)
End If
Me.gpxPatientDetails.Text = " Age:" + Years.ToString()
Upvotes: 0
Views: 2839
Reputation: 11
A perfectly working VB.NET code for calculating age using "DateTimePicker" and "TextBox". Gives exact calculation for both Leap years and normal years.
Function CalculateDOB(DOB As Date) As String
Try
If pkDOB.Value <> Date.Today Then
Dim DOBYear As Integer = pkDOB.Value.Year
Dim DOBMonth As Integer = pkDOB.Value.Month
Dim DOBDay As Integer = pkDOB.Value.Day
Dim CurrentYear As Integer = Date.Today.Year
Dim CurrentMonth As Integer = Date.Today.Month
Dim CurrentDay As Integer = Date.Today.Day
Dim MonthEndings() As Integer = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
If DOBDay > CurrentDay Then
CurrentMonth = CurrentMonth - 1
If (CurrentYear%4!=0) Then
CurrentDay = CurrentDay + MonthEndings (DOBMonth - 1)
Else
CurrentDay = CurrentDay + MonthEndings(DOBMonth)
End If
End If
If DOBMonth > CurrentMonth Then
CurrentYear = CurrentYear - 1
CurrentMonth = CurrentMonth + 12
End If
Dim AgeDay As Integer = CurrentDay - DOBDay
Dim AgeMonth As Integer = CurrentMonth - DOBMonth
Dim AgeYear As Integer = CurrentYear - DOBYear
Dim age As String = AgeYear & "y" & AgeMonth & "m" & AgeDay & "d"
txtAge.Text = age
End If
Catch ex as exception
MessageBox.Show(ex.Message)
End Try
End Function
Upvotes: 1
Reputation: 5139
A bit old posting, but the question is not! Typically the 'age' is the whole years completed:
Dim DOB As Date = DateValue("1970-10-28")
Dim Tday As Date = DateValue("2022-02-21")
Console.WriteLine(
Int((CInt(Tday.ToString("yyyyMMdd")) - CInt(DOB.ToString("yyyyMMdd"))) / 10000)
)
Upvotes: 0
Reputation: 11
Function AgeCalculator(ByVal FromDate As String, Optional flgyearOnly As Boolean = False) As String '23/11/2017
If Not IsDate(FromDate) Then Return ""
Dim tmpYear As String = "", tmpMonth As String = "", tmpdiff As Integer = 0
Dim tmpAge As String = ""
tmpdiff = DateDiff(DateInterval.Day, CDate(Format2DateMMM(FromDate)), Now)
If tmpdiff <= 0 Then Return ""
If tmpdiff > 0 And tmpdiff <= 29 Then
Return tmpdiff & " Days"
End If
If tmpdiff = 30 Then
Return " 1 Month"
End If
tmpYear = tmpdiff / 365
If InStr(tmpYear, ".") > 0 Then
tmpYear = Microsoft.VisualBasic.Left(tmpYear, InStr(tmpYear, ".") - 1)
End If
If Val(tmpYear) = 0 Then tmpYear = ""
If Val(tmpYear) > 0 Then
tmpAge = tmpYear & " years"
If flgyearOnly Then Return tmpAge
End If
tmpdiff = (tmpdiff - (Val(tmpYear) * 365))
tmpMonth = tmpdiff / 30
If InStr(tmpMonth, ".") > 0 Then
tmpMonth = Microsoft.VisualBasic.Left(tmpMonth, InStr(tmpMonth, ".") - 1)
End If
If Val(tmpMonth) = 0 Then tmpMonth = ""
If tmpMonth > 0 Then
tmpAge &= " " & tmpMonth & " Months"
End If
tmpdiff = (tmpdiff - (Val(tmpMonth) * 30))
If Val(tmpdiff) > 0 Then
tmpAge &= " " & tmpdiff & " Days"
End If
Return tmpAge: End Function
Upvotes: 1
Reputation: 11
If you have problem with bigger/smaller age, because someone doesn't have a birthday this year (born in December for example), you can try this:
Dim bday As New DateTime(2010, 1, 25)
Dim months As Integer = DateDiff(DateInterval.Month, bday, Now)
Dim years As Integer = months / 12
Upvotes: 1
Reputation: 888
I think you want to have the difference of the date, I think this example help you:
Dim birthday As New DateTime(12, 12, 2012)
Dim difference As DateTime = DateTime.Now - birthday
Dim years As Integer = difference.Years
Upvotes: 1