Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can you give me an example how to compute the age of person using vb? 3

Status
Not open for further replies.
Jul 10, 2002
5
PH
can you give me an example using visual basic how to compute the age of a person by using his birtdate and present year? Example September 25,1974 and the present date is July 12,2002 my age is = 27 . If September 25,1974 and the present date is September 25,2002 my age will be 28. Is that possible?
 
Here you go

Private Sub Command1_Click()
Dim BirthDate As Date

BirthDate = #5/8/1969#
Command1.Caption = DateDiff("yyyy", BirthDate, Now)
End Sub

Hope this helps [spin] If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]
 
Unfortunately, the above will not work. The DateDiff function always will return a value rounded to the interval used. So, in the above example, where the current date is July 12,2002, the datediff will return 28, and not 27. It would only return 27 up till 31 Dec. 2001.

The same problem exists when the age is under 1 year:
Birthdate = Dec 31,2001
Current Date = Jan 01, 2002
DateDiff("yyyy", BirthDate, Now) will return 1 year even though the baby is only 1 day old.

This answer should work:

Function CalcAge(BirthDate As Date) As Integer
Dim Age As Integer
If IsDate(BirthDate) Then
Age = DateDiff("yyyy", BirthDate, Now)
If Date < DateSerial(Year(Now), Month(BirthDate), Day(BirthDate)) Then Age = Age - 1
End If
CalcAge = Age
End Function


I made the variable BirthDate a Date type variable so that the date can be passed as a variable declared as a Date or String.
Debug.Print CalcAge(&quot;22 Jul,60&quot;)
Or Debug.Print CalcAge(&quot;7/22/1960&quot;)

= 41
 
CCLINT,

I like the approach, but often it is useful to know the age &quot;As Of&quot; some date, so I added an optional parameter to permit this option. While in the midst of it all, I also replaced the double logical computation of the &quot;offset&quot; to a single one.

Code:
Function CalcAge(BirthDate As Date, Optional AsOf As Variant) As Integer

    If (IsMissing(AsOf)) Then
        AsOf = Date
    End If

    If (Not IsDate(AsOf)) Then
        AsOf = Date
    End If

    If (IsDate(BirthDate)) Then
        CalcAge = DateDiff(&quot;yyyy&quot;, BirthDate, AsOf) _
                  + (AsOf < DateSerial(Year(AsOf), Month(BirthDate), Day(BirthDate)))
     Else
        CalcAge = -1
    End If

End Function
MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Good.
I do it also similar, but for simplicity in understanding, it is sometimes better to show both ways (concerning the offset).
As far as the &quot;AsOf&quot; date, I do this to, but I declare both as strings and just check the values with IsDate. (I do this because all the dates that I use are stored in string variables (in the db as dates though). Alot less problems, considering the past ones, especially working internationally).

I try not to use variants if not needed. Here, it is not needed....Just needs to be written differently.

I did make a mistake and didn't pass ByVal - it is being passed by reference. Which will cause problems if you pass a string variable as the Birthdate, or pass a zero length.

Therefore, declare both as strings, and ByVal, then you can pass what you want(Dates, strings, date serial, or even numbers:

Function CalcAge(ByVal BirthDate As String, Optional ByVal AsOf As String) As Integer

If (Not IsDate(AsOf)) Then
AsOf = Date
End If

If (IsDate(BirthDate)) Then
CalcAge = DateDiff(&quot;yyyy&quot;, BirthDate, AsOf) _
+ (AsOf < DateSerial(Year(AsOf), Month(BirthDate), Day(BirthDate)))
Else
CalcAge = -1
End If

End Function

?CalcAge(DateAdd(&quot;d&quot;,-364,date))
=0
?CalcAge(DateAdd(&quot;d&quot;,-365,date))
=1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top