calculate age

7anthala

Registered User.
Local time
Tomorrow, 01:13
Joined
Apr 7, 2014
Messages
41
i have this code for calculating the age from birthdate
but i want if there is years no months or days appear

Public Function CalcAge(Birthdate As Date) As String
Dim intYears As Integer, intMonths As Integer, intDays As Integer
intMonths = DateDiff("m", Birthdate, Date)
intDays = DateDiff("d", DateAdd("m", intMonths, Birthdate), Date)
If intDays < 0 Then
intMonths = intMonths - 1
intDays = DateDiff("d", DateAdd("m", intMonths, Birthdate), Date)
End If
intYears = intMonths \ 12
intMonths = intMonths Mod 12
CalcAge = intYears & " Y" _
& ", " & intMonths & " M" _
& ", " & intDays & " D"
End Function
 
I found the following code on a Microsoft site. It seems to give the result you want:
Code:
' FUNCTION NAME: Age()
'
' PURPOSE:
'    Calculates age in years from a specified date to today's date.
'
' INPUT PARAMETERS:
'    StartDate: The beginning date (for example, a birth date).
'
' RETURN
'    Age in years.
'
'*************************************************************
Function Age(varBirthDate As Variant) As Integer
   Dim varAge As Variant

   If IsNull(varBirthDate) Then Age = 0: Exit Function

   varAge = DateDiff("yyyy", varBirthDate, Now)
   If Date < DateSerial(Year(Now), Month(varBirthDate), _
                        Day(varBirthDate)) Then
      varAge = varAge - 1
   End If
   Age = CInt(varAge)
End Function
 
I answered this question for you last week.
 
i have this code for calculating the age from birthdate
but i want if there is years no months or days appear

CalcAge = intYears & " Y" _
& ", " & intMonths & " M" _
& ", " & intDays & " D"
End Function

If you are saying that if the age is less than 1 year then you want month and days otherwise just the years then a simple If block will do it

If IntYears = 0 Then
CalcAge = intMonths & " M" & ", " & intDays & " D"
Else
CalcAge = intYears & " Y"
End If

Brian
 
thanks for ur help guys
Brianwarnock it worked great
 

Users who are viewing this thread

Back
Top Bottom