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

How do I calculate someone's age from their Date of Birth? 4

Status
Not open for further replies.

wniatbms

Programmer
Dec 5, 2003
24
0
0
ZA
Good day.

Let's say part of the registration process on my website includes your date of birth. I want to be able to show that users age.

How do I do this?

I am having the following problem:

Lets say the users DOB is 15-Aug-1978. My first thought was to just subtract the DOB from the current date. But NNNOOO!

I get 26. It should be 25 until Sunday. How do I work around this. Any help would be most appreciated.

Thank you.
 
Code:
Function GetAge(dtDOB)
	
		IF Not isDate(cDate(dtDOB)) Then
			GetAge = -1
		Else
			dtDOB = cDate(dtDOB)
		End IF
		
		Dim dtTemp, dtNow
		dtTemp = FormatDateTime(dtDOB, 2)
		dtNow = FormatDateTime(Now(), 2)
		
		IF dtDOB > dtNow Then
			GetAge = 0
		End IF
		
		GetAge = DateDiff("yyyy",dtTemp, dtNow, 0)
		
		
	End Function
	
	Response.Write(GetAge("6/15/1973"))
 
Sorry

I Screwed up.

This works:
Code:
	Function GetAge(dtDOB)
	
		IF Not isDate(cDate(dtDOB)) Then
			GetAge = -1
		Else
			dtDOB = cDate(dtDOB)
		End IF
		
		Dim dtTemp, dtNow
		dtDOB = FormatDateTime(dtDOB, 2)
		dtNow = FormatDateTime(Now(), 2)
		
		IF dtDOB > dtNow Then
			GetAge = 0
		End IF
		
		dtTemp = DateDiff("yyyy", dtDOB, dtNow, 1)
		
		Dim lCurrentMonth, lDOBMonth, lCurrentDay, lDOBDay
		
		lCurrentMonth = DatePart("m", dtNow, 0, 0)
		lDOBMonth = DatePart("m", dtDOB, 0, 0)
		lCurrentDay = DatePart("d", dtNow, 0, 0)
		lDOBDay = DatePart("d", dtDOB, 0, 0)
		
		IF (lCurrentMonth >= iDOBMonth) AND (lCurrentDay < lDOBDay) Then
			GetAge = (dtTemp - 1)
		Else
			GetAge = dtTemp
		End IF
		
		
	End Function
	
	Response.Write(GetAge("8/13/1973"))
 
Sorry Again
Remind me never to try this at 2:00 am again.

This one really works, seriously. Please disregard that previous 2 posts.

Code:
	Function GetAge(dtDOB)
	
		IF Not isDate(cDate(dtDOB)) Then
			GetAge = -1
		Else
			dtDOB = cDate(dtDOB)
		End IF
		
		Dim dtTemp, dtNow
		dtDOB = FormatDateTime(dtDOB, 2)
		dtNow = FormatDateTime(Now(), 2)
		
		IF dtDOB > dtNow Then
			GetAge = 0
		End IF
		
		'dtDOB = DateDiff("yyyy", dtDOB, dtNow, 1)
		
		Dim lCurrentMonth, lDOBMonth, lCurrentDay, lDOBDay
		
		lCurrentMonth = DatePart("m", dtNow, 1, 1)
		lDOBMonth = DatePart("m", dtDOB, 1, 1)
		lCurrentDay = DatePart("d", dtNow, 1, 1)
		lDOBDay = DatePart("d", dtDOB, 1, 1)
		lCurrentYear = DatePart("yyyy", dtNow, 1, 1)
		lDOBYear = DatePart("yyyy", dtDOB, 1, 1)
		
		dtDOB = DateDiff("yyyy", dtDOB, dtNow, 1)
		
		Dim lYears
		lYears = (lCurrentYear - lDOBYear)
		
		IF (lCurrentMonth < lDOBMonth) Then
			GetAge = (cInt(dtDOB) - 1)
		ElseIF (lCurrentMonth <= lDOBMonth) AND (lCurrentDay < lDOBDay) Then
			GetAge = (cInt(dtDOB) - 1)
		Else
			GetAge = dtDOB 
		End IF
		
	End Function
	
	Response.Write(GetAge("8/13/1973"))
 
JSpicolli

Do you know what I love about the Tek-Tips website? People like you!!!!

You rock!

Thank you so much for your help. You saved my @$$.

I know all about the 2am thing. You have my support 150%.

Thank you so much.
 
I am completly new to asp programming. I tried to paste the code in a page and I do not get anything back on the page. Do I need something before the function statement? Anyhelp would be greatly appreciated.
 
Show us the code you tried...did u save your page as .asp?

-L
 
Here is what I tried. I did save it as asp.

<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<meta name="Microsoft Theme" content="ice23 011, default">
</head>

<body>

</body>

</html>
<%
Function GetAge(dtDOB)

IF Not isDate(cDate(dtDOB)) Then
GetAge = -1
Else
dtDOB = cDate(dtDOB)
End IF

Dim dtTemp, dtNow
dtDOB = FormatDateTime(dtDOB, 2)
dtNow = FormatDateTime(Now(), 2)

IF dtDOB > dtNow Then
GetAge = 0
End IF

'dtDOB = DateDiff("yyyy", dtDOB, dtNow, 1)

Dim lCurrentMonth, lDOBMonth, lCurrentDay, lDOBDay

lCurrentMonth = DatePart("m", dtNow, 1, 1)
lDOBMonth = DatePart("m", dtDOB, 1, 1)
lCurrentDay = DatePart("d", dtNow, 1, 1)
lDOBDay = DatePart("d", dtDOB, 1, 1)
lCurrentYear = DatePart("yyyy", dtNow, 1, 1)
lDOBYear = DatePart("yyyy", dtDOB, 1, 1)

dtDOB = DateDiff("yyyy", dtDOB, dtNow, 1)

Dim lYears
lYears = (lCurrentYear - lDOBYear)

IF (lCurrentMonth < lDOBMonth) Then
GetAge = (cInt(dtDOB) - 1)
ElseIF (lCurrentMonth <= lDOBMonth) AND (lCurrentDay < lDOBDay) Then
GetAge = (cInt(dtDOB) - 1)
Else
GetAge = dtDOB
End IF

End Function


Response.Write(GetAge("8/13/1973"))
%>
 
Try this:
Code:
<html>

<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<meta name="Microsoft Theme" content="ice23 011, default">
</head>

<body>
<%
Function GetAge(dtDOB)
    
        IF Not isDate(cDate(dtDOB)) Then
            GetAge = -1
        Else
            dtDOB = cDate(dtDOB)
        End IF
        
        Dim dtTemp, dtNow
        dtDOB = FormatDateTime(dtDOB, 2)
        dtNow = FormatDateTime(Now(), 2)
        
        IF dtDOB > dtNow Then
            GetAge = 0
        End IF
        
        'dtDOB = DateDiff("yyyy", dtDOB, dtNow, 1)
        
        Dim lCurrentMonth, lDOBMonth, lCurrentDay, lDOBDay
        
        lCurrentMonth = DatePart("m", dtNow, 1, 1)
        lDOBMonth = DatePart("m", dtDOB, 1, 1)
        lCurrentDay = DatePart("d", dtNow, 1, 1)
        lDOBDay = DatePart("d", dtDOB, 1, 1)
        lCurrentYear = DatePart("yyyy", dtNow, 1, 1)
        lDOBYear = DatePart("yyyy", dtDOB, 1, 1)
        
        dtDOB = DateDiff("yyyy", dtDOB, dtNow, 1)
        
        Dim lYears
        lYears = (lCurrentYear - lDOBYear)
        
        IF (lCurrentMonth < lDOBMonth) Then
            GetAge = (cInt(dtDOB) - 1)
        ElseIF (lCurrentMonth <= lDOBMonth) AND (lCurrentDay < lDOBDay) Then
            GetAge = (cInt(dtDOB) - 1)
        Else
            GetAge = dtDOB 
        End IF
        
    End Function
    
  
  Response.Write(GetAge("8/13/1973"))
  %>
</body>

</html>

Had put everything inside the body...

-L
 
I copied the code in the page saved it as asp and I still get no errors, but nothing comes back to the page in preview mode.

Thanks for being patient
 
I do not have iis running on my xp machine yet. I did move the asp page to my web site to test and the page came up with nothing on it.

I will install iis on my machine and try it on Monday.

Thanks for all of the support!!!
 
Something to consider, but if you are running XP Home, it does not come with IIS. Only XP Pro has IIS. If you have XP Pro, then ignore me. ;-)

------------------------------------------------------------------------------------------------------------------------
If you don't have a sense of humor, you probably don't have any sense at all.
- Anonymous
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top