# Find the age of a person..

How to find the age of a person by providing date of birth using VBScript?
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
@t_msenthilk:

> How to find the age of a person by providing date of birth using VBScript?

Dim nAge
Dim dtDOB

nAge = (Now() - dtDOB) / 365.24

(Substracting 2 date values gives you their difference in Days.  Divide that by the number of days in a year, and you get years...)  If you a nice round number, you can just round that value.

Hope That Helps,
Dex*
0
Commented:
0
Project managerCommented:
' ******************************************************************
' This function returns a person's age in years. It addresses some
'  rounding issues with using a simple DateDiff() function call.

' Example: lngYourAge = Age("04/24/1973")
' Returns: lngYourAge is the correct age of the person as of today

' By: Craig H. Rettig
' Date: February 2003

' Get more free code samples at http://www.BitBucketHeaven.com/
' ******************************************************************

Public Function Age(ByVal vntDOB As Variant, _
Optional ByVal vntAsOf As Variant = Null) As Long

Dim dtDOB As Date
Dim dtAsOf As Date
Dim dtBDay As Date  'Birthday in the year of calculation.

Age = -1 'Initialize to -1 to return if an error occurs.
If IsDate(vntDOB) Then
dtDOB = CDate(vntDOB)

If Not IsDate(vntAsOf) Then 'Date to calculate age from.
dtAsOf = Date
Else
dtAsOf = CDate(vntAsOf)
End If

If dtAsOf >= dtDOB Then 'Calculate only if it's after person was born.
dtBDay = DateSerial(Year(dtAsOf), Month(dtDOB), Day(dtDOB))
Age = CLng(DateDiff("yyyy", dtDOB, dtAsOf) + (dtBDay > dtAsOf))
End If
End If
End Function

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Why would you use that huge function when you can do it in one line?  If you really like the "DateDiff" function, then use it like this:

nAge = DateDiff("yyyy", dtDOB, Date())

There is no reason for that function to be so much code.

Dex*
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.