Click to See Complete Forum and Search --> : Help: Calculate Age with ASP


highv0ltage
06-04-2004, 10:30 AM
I am trying to construct a script that will take someone's birthdate, compare it to today's date and calculate the person's age. Here's what I have so far:


birthday = request.form("bday")
birthmonth = request.form("bmonth")
birthyear = request.form("byear")
strDOB = birthday & "/" & birthmonth & "/" & birthyear
DOB = CDate(strDOB)
iAge = CInt(dateDiff("yyyy",DOB,now()))
IF (DOB < now()) THEN
iAge = iAge - 1
END IF
age = iAge


This doesn't seem to work entirely correct. Can anyone help me out? Thanks.

It just seems to determine the age by the year and doesn't take into consideration whether or not the person was born before or after todays month and day.

CrazyC115
06-04-2004, 03:24 PM
Originally posted by highv0ltage
I am trying to construct a script that will take someone's birthdate, compare it to today's date and calculate the person's age. Here's what I have so far:


birthday = request.form("bday")
birthmonth = request.form("bmonth")
birthyear = request.form("byear")
strDOB = birthday & "/" & birthmonth & "/" & birthyear
DOB = CDate(strDOB)
iAge = CInt(dateDiff("yyyy",DOB,now()))
IF (DOB < now()) THEN
iAge = iAge - 1
END IF
age = iAge


This doesn't seem to work entirely correct. Can anyone help me out? Thanks.

It just seems to determine the age by the year and doesn't take into consideration whether or not the person was born before or after todays month and day.

Instead of using DateDiff to get the difference in years use it to get the difference in days "d".

Then all you have to do is divide out the years, months and days and you can print out something like:

You are currently 24 Years, 5 Months and 12 Days old.

If they were born yesteday then you can say:

You are currently 1 Day old. You really were born yesterday weren't you?

Or:

You were born 2 Months and 3 Days ago!

etc.