faqts : Computers : Programming : Languages : Asp : ASP/VBScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

46 of 105 people (44%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How can I calculate the age of a person from a date of birth entered in a form using format xx/xx/xx - Using DateDiff returns only years not age

Jan 15th, 2002 01:10
Anton Channing, jim m, Scott Fleming, Dave Wooldridge,


>The following example should work better...
>
>dtDOB = Request.Form("DOB")
>
>iAge = CInt(DateDiff("yyyy",FormatDateTime(dtDOB,1),Date()))
>dtTmp = CDate((Day(dtDOB) & "/" & Month(dtDOB) & "/" & Year(Date())))
>if (dtTmp => Date) then iAge = iAge - 1
>
>Response.Write "Age is " & iAge
I hate to reply to myself, but I left a bug in
the above code.  This will display the wrong age
if someones birthday is today.  Below is the corrected
version, tidied up and put in a neat little
function...
function getAge(dtDOB)
	dim iAge, dtTmp, dtToday
	dtToday = Date()
	iAge = CInt(DateDiff("yyyy",FormatDateTime(dtDOB,1),Date()))
	dtTmp = CDate((Day(dtDOB) & "/" & Month(dtDOB) & "/" & Year(Date
())))
	if (dtTmp > Date) then iAge = iAge - 1
	getAge = iAge
end function
dim DOB
DOB = "15/01/1974"
Response.Write " 1. DoB is " & DOB
Response.Write " <BR>2. Age is " & getAge(DOB)



© 1999-2004 Synop Pty Ltd