I’m hoping someone might be able to help me please?
I’m sure this is simple to do, but I’m not very clued up in ASP…
What I’m looking for is some code where I can hard code someone’s date of birth in, and it will show (on the page), their age - obviously working it out from todays date.
Would someone be able to supply me with some code please? And maybe explain what’s what so I can try and understand it?
This is the script i use to calculate age. It’s a SQL User Defined Function, not asp.
CREATE FUNCTION GetAge
/*
Version 1.1
This function returns the age of a person, in years, for a given date
of birth. Due to the limitations placed on using the GETDATE() function
within user-defined functions, todays date also needs to be supplied. Created by Karl Grambow. [email protected].**
*/
(@DateOfBirth datetime, @Today datetime)
RETURNS int
AS
BEGIN
DECLARE @Age int --Age in years.
SET @Age = YEAR(@Today) - YEAR(@DateOfBirth)
IF MONTH(@DateOfBirth) > MONTH(@Today)
BEGIN
SET @Age = @Age - 1
END
IF MONTH(@DateOfBirth) = MONTH(@Today)
BEGIN
IF DAY(@DateOfBirth) > DAY(@Today)
BEGIN
SET @Age = @Age - 1
END
END
RETURN (@Age)
END
A bit crude, as it hard codes the date as you requested. You could use any variable (recordset value,request.form, request.querystring) you like to define the value of bdate.
I’ve included some notes in the code to help explain things
Here’s the code:
[CODE]<%@LANGUAGE=“VBSCRIPT” CODEPAGE=“1252”%>
<%
Dim bdate
'Define the birthdate
bdate = cdate(“01-01-2001”)
Dim age
'Subtract the birthdate from today’s date and convert it to an integer
age = cint(now() - bdate)
'Divide by 365 to get the number of years
age = age/365
%>
I’ve just tried hafa’s, but it seems to act a bit strange.
For example, my birthday is 24th August 1984, so I placed this as a date, and it showed me as 21 - correct! However, if I changed today’s month to Feb, it shows me as 22, and same with March, April etc etc.
I’ve just tried hafa’s, but it seems to act a bit strange.
For example, my birthday is 24th August 1984, so I placed this as a date, and it showed me as 21 - correct! However, if I changed today’s month to Feb, it shows me as 22, and same with March, April etc etc.
It doesn’t seem to be spot on :-
[/QUOTE]
The script is rounding the number, as noted. You could use the following to have it display 2 decimal places:
<%=FormatNumber((age), 2, -2, -2, -2) %>
Alternately, you could do a bit of math and logic with the “age” variable to define the value of the integer based on its location within a 12-month period.
hehe, sometimes the simplest things are the hardest to spot The functions list on Hanengs is really handy, especially when you forget the syntax to something.
[code] Function AgeCalc(dtmUserDOB)
Dim intUserAge
intUserAge = Year(Now) - Year(dtmUserDOB)
If Month(Now) = Month(dtmUserDOB) Then
if Day(dtmUserDOB) > Day(NOW()) Then
intUserAge = (intUserAge - 1)
end if
elseif Month(dtmUserDOB) > Month(Now) then
intUserAge = (intUserAge - 1)
End If
AgeCalc = intUserAge
End Function[/code]
to use this, place it into the site then put
intMemberAge = AgeCalc(dtmDOB)
obviously you’d change dtmDOB to what ever the date of birth is (maybe from a database?)
on the site i linked to, am using one i built my self
but as am used to using WebWizForum the look & layout is based on that, with tweeks that i profer (eg i did threads MSN Groups style - where the first/original post is seperate)
That’s awesome that you built yours, looks very nice. Yes I like Web WIz look too.
Guess since you built yours I have a question. What file in the web wiz forum can I change the color of the background of the drop down select boxes and regular imput text boxes?
[QUOTE=WebDesignSlave]
That’s awesome that you built yours, looks very nice. Yes I like Web WIz look too.
Guess since you built yours I have a question. What file in the web wiz forum can I change the color of the background of the drop down select boxes and regular imput text boxes?
Is there a setting that I might of screwed up on, that won’t let me log out of the forum, it keeps me sign in. Everytime I go to log out it won’t let me.