J
John
Hi
How can I calculate age from a given DOB?
Thanks
Regards
How can I calculate age from a given DOB?
Thanks
Regards
John said:Hi
How can I calculate age from a given DOB?
Thanks
Regards
I would use something akin to the following:
Sub Main()
Dim DOB As Date = New Date(1925, 8, 4) ' use your favorite date
Dim Today As Date = Date.Now
Dim age As Integer = Today.Year - DOB.Year
If (Today.Month < DOB.Month) Then age = age - 1
If (Today.Month = DOB.Month) And (Today.Day < DOB.Day) Then _
age = age - 1
Console.WriteLine("Age: " & age)
Console.ReadLine()
End Sub
Hope this helps...
Spam Catcher said:Can you just go Now.Subtract(DOB).Year?
Family said:That was my first thought, but my TimeSpan object doesn't have a Year
property.
That was my first thought, but my TimeSpan object doesn't have a Year
property.
Probably just as well, it wouldn't know how many years X number of days is
anyway.
If you want a one line version, and don't mind adding a boolean expression, you
could write
Dim age As Integer = Now.Year - DOB.Year + (New Date(Now.Year, DOB.Month,
DOB.Day) > Now)
Chris said:And this compiles with option strict on? It makes no sense to add a
boolean to an integer!