Solved

Find the age of a person..

Posted on 2003-11-30
4
3,426 Views
Last Modified: 2008-04-23
How to find the age of a person by providing date of birth using VBScript?
0
Comment
Question by:t_msenthilk
  • 2
4 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 9848553
@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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9848585
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 150 total points
ID: 9848631
' ******************************************************************
' 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
 
LVL 19

Expert Comment

by:Dexstar
ID: 9850394
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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hibernate methods 2 72
linearIn  challenge 23 107
mapAB Challlenge 35 145
What language/protocol is the Angular Chat? 2 77
The purpose of this article is to demonstrate how we can use conditional statements using Python.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question