Age Calculation Error

1. Junior Member
Join Date
Dec 1969
Posts
12

## Age Calculation Error

Hi. I&#039;m using the following script to determine a child&#039;s age at the beginning of the school year (9/1/2003). There&#039;s a bug in it somewhere though and I can&#039;t seem to figure it out. If the child&#039;s birthdate (child_dob) is anywhere from 01/01/xxxx to 08/31/xxxx then the age is calculated correctly. BUT if the child was born between 9/1/xxxx and 12/31/xxxx the results say that the child is 1 year older than they actually are. Hopefully I&#039;m just missing some silly mistake in the script. Any help would be appreciated.<BR><BR>&#039;************************ *********************<BR>&#039;* AGE CALCULATION AS OF PROGRAM YEAR *<BR>&#039;*************************************** ******<BR>&#039;calculate Age in Years as of Program Year Start Date<BR>Dim varAge<BR>DOB = child_dob<BR><BR> If DOB = NULL then Age = 0<BR><BR> varAge = DateDiff("yyyy", DOB, ProgramYear)<BR> If Date &#060; DateSerial(Year(ProgramYear), Month(DOB), _<BR> Day(DOB)) Then<BR> varAge = varAge - 1<BR> End If<BR>Age = CInt(varAge)<BR><BR>&#039;calculate months since last birthdate<BR>StartDate = DOB<BR><BR>Dim tAge<BR> tAge = (DateDiff("m", StartDate, ProgramYear))<BR> If (DatePart("d", StartDate) &#062; DatePart("d", ProgramYear)) Then<BR> tAge = tAge - 1<BR> End If<BR> If tAge &#060; 0 Then<BR> tAge = tAge + 1<BR> End If<BR><BR> AgeMonths = CInt(tAge Mod 12)<BR><BR>&#039;********************************* ************<BR>&#039;* END AGE CALCULATION *<BR>&#039;*************************************** ******

2. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: Age Calculation Error

This is the method I use<BR><BR>if month(date) &#062; month(dob) then<BR> age =datediff("yyyy", dob , date)<BR>elseif month(date) = month(dob) then<BR> if day(date) &#062;= day(dob) then<BR> age = datediff("yyyy", dob , date)<BR> else<BR> age = datediff("yyyy", dob , date)-1<BR> end if<BR>else<BR> age = datediff("yyyy", dob , date)-1<BR>end if<BR>response.write age<BR><BR><BR><BR>of course if you have the info in a SQL Server DB you can calculate it using SQL<BR><BR>select CASE<BR>WHEN dateadd(year, datediff (year, d1, d2), d1) &#062; d2<BR>THEN datediff (year, d1, d2) - 1<BR>ELSE datediff (year, d1, d2)<BR>END as Age<BR>

3. Senior Member
Join Date
Dec 1969
Posts
11,334

## Count the days

it&#039;s more accurate...<BR><BR>mydays = datediff("d", now, #9/1/1993#)<BR><BR>myage = mydays MOD 365 &#039;close enough... screw the leap years<BR><BR>Untested, but more accurate than using the year.

4. Senior Member
Join Date
Dec 1969
Posts
11,334

## Oops

Try this instead:<BR><BR>myage = mydays 365

5. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: Oops

Hmmm... leap years do screw it up. I guess try WK&#039;s solution then.

6. Senior Member
Join Date
Dec 1969
Posts
11,247

## I was just about to Hammer you!!

But now you are forgiven -- ;-)<BR><BR>

7. Junior Member
Join Date
Dec 1969
Posts
12

## RE: Age Calculation Error

The data isn&#039;t in SQL Server just yet - the age determines whether the data will be entered into the DB.<BR><BR>Your first method seems to work well and looks a lot cleaner than my mess of code but I need to know the age in years + months. (i.e. child is 5 years, 4 months old.) For simplicity I could just use the total age in months (i.e. child is 64 months old). How would I modify that to calculate months instead?<BR><BR>I&#039;ve been staring at this code for so long that it&#039;s burned into my retinas!

8. Junior Member
Join Date
Dec 1969
Posts
12

## RE: Oops

Thanks for the effort! Unfortunately this has to be an exact age so leap years would have to be taken into consideration. :-)

9. Senior Member
Join Date
Dec 1969
Posts
11,247

## RE: Age Calculation Error

Ok now hitting unknown area But try this<BR><BR>if month(date) &#062; month(dob) then<BR> age =datediff("m", dob , date)<BR>elseif month(date) = month(dob) then<BR> if day(date) &#062;= day(dob) then<BR> age = datediff("m", dob , date)<BR> else<BR> age = datediff("m", dob , date)-1<BR> end if<BR>else<BR> age = datediff("m", dob , date)-1<BR>end if<BR>response.write age

10. Junior Member
Join Date
Dec 1969
Posts
12

## RE: Age Calculation Error

Wow! That&#039;s awesome!<BR><BR>Using a test dob of 10/4/98 I got a result of 65 months old, but as of today that child would be 66 months old. If I remove the "-1"s it seems to give the right result but I&#039;ll have to test some other dates to see if I didn&#039;t screw something else up.<BR><BR>Thanks for your help!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•