Function GetAge(date1,date2)
Dim tmpage, leapBaby, ageInYears, date1alt
leapBaby = false
' use DateSerial(y,m,d) to avoid locale issues
date1 = DateSerial(datepart("yyyy",date1),datepart("m",date1),datepart("d",date1))
date2 = DateSerial(datepart("yyyy",date2),datepart("m",date2),datepart("d",date2))
' make sure we have a valid date!
if date2 >= date1 then
' determine if leapYearBaby
if month(date1) = 2 and day(date1) = 29 then
leapBaby = true
end if
' get absolute number of years
ageInYears = cint(datediff("YYYY", date1, date2))
' get date1's month and day in terms of date2's year
date1alt = dateadd("yyyy", ageInYears, date1)
if date1alt > date2 then
' their birthday hasn't hit yet in date2's year
ageInYears = ageInYears - 1
end if
if leapBaby = true then
' need to format output slightly
yearsPassed = ageInYears
ageInYears = ageInYears \ 4
end if
if leapBaby = false then
tmpage = ageInYears
Else
'leap baby
tmpage = yearsPassed
End If
GetAge = tmpage
else
'bad date
end if
End Function