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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating an age using VBScript

Status
Not open for further replies.

mmcgon

Programmer
Jan 12, 2003
29
0
0
US
Anyone have a slick way to calculate a person's age using VBScript given their birth date and today's date?

Using DateDiff just subtracts the year value, so if I were born on 12/31/02 DateDiff says I'm 1 (2003-2002), and on 1/1/03 it says I'm 0 (2003-2003).
 
Doing this gives me my right age
DateDiff("yyyy","9/27/1980",Date)
 
My system gives me 23 with that code. But you should'nt be 23 until later this month on 9/27.
 
What if you selected the number of days between the two, divided by 365 and took the integer of that number.

int(datediff("d", "9/27/1980", date) / 365)

This will give you 22.

The only question I'm wondering about is leap year and how that would affect this.

Try this and see if it will work for what you are doing.

Bernie
 
You could try using something like the following. It's a snippet of code I added to a free forum that I downloaded.
Code:
strDOB = rsBirthDayMod("DOB")
   Age = DateDiff("yyyy", strDOB, Now)
   If Date < DateSerial(Year(Now), Month(strDOB), Day(strDOB)) Then
      Age = Age - 1
   End If
   Age = CInt(Age)
Response.Write(&quot;(&quot; & Age & &quot;)&quot;)
rsBirthDayMod.MoveNext
If NOT rsBirthDayMod.EOF Then Response.Write(&quot;, &quot;)
Loop
End If
 
check out this thread: thread183-610239
its how to do this in SQL-Server.

Craigey's solution seems decent if you just want a whole number.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 

_____________________________________________________________________
[sub]Where have all my friends gone to????[/sub]
onpnt2.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top