Age frm Birthday script?

Hi,

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?

Thanks for your time!

Oli

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
%>

Untitled Document <%=FormatNumber((age), 0, -2, -2, -2) %> [/CODE]

You can download the code at http://angildesign.com/bday.zip

Thanks to you both for the code!

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=OJM]
Thanks to you both for the code!

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.

Why not just use ASPs built in datediff function: datediff(“yyyy”, birthday, now())

Check here for more info.

[QUOTE=Padwah]
Why not just use ASPs built in datediff function: datediff(“yyyy”, birthday, now())

Check here for more info.
[/QUOTE]

Because that would be far too easy? Thanks for the link; bookmarked.

hehe, sometimes the simplest things are the hardest to spot :slight_smile: The functions list on Hanengs is really handy, especially when you forget the syntax to something.

acctually that doesnt work, as it doesnt seem to take into account the month or day

i wrote a function for my site

here it is:

[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?)

i use Microsoft’s site for functions and syntax

(down side is they recently changed it, and the new site doesnt seem to work with FireFox X(, so you’ll need to open it up in IE ;()

What forum are you using Scotty?? Looks good…

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?

Thanks Scotty.

Thanks Scotty, however, what format do I need to enter it in?

Ie, for 27th January 1984, does it need to read;

intMemberAge = AgeCalc(27/01/1984)

I can’t seem to get it to work…it just doesn’t show anything.

[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?

Thanks Scotty.
[/QUOTE]

straight off, id say use CSS

for text boxs use

<style> input { background: #FFFFFF; color: #000000;} </style>

id set a text color too

for drop down boxs use

<style> select { background: #000000; color: #FFFFFF;} </style>

Oh ok, I was thinking I had to find the code in the .asp pages and tweak there.

Thanks :wink:

it depends on your server which format you put it in

i think on jodos servers it goes mm/dd/yyyy

if your going to add it manually, you’ll need to do:

intMemberAge = AgeCalc("01/27[B]/1984")

[/B]if your takin it from a database, i doubt you’ll need to change the format, but you can use do this to make it a correct format

dtmDOB = CDate(DateSerial(YYYY, MM, DD))

and and it should be fine

[QUOTE=WebDesignSlave]
Oh ok, I was thinking I had to find the code in the .asp pages and tweak there.

Thanks :wink:
[/QUOTE]

forgot to say

if your adding it to webwizforum just find the Default_style.css file in forum/includes/ and add anywhere in that file

Yes that worked thanks a bunch.

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.

i seriously doubt you broke your forum by editing the CSS file

could try reuploading the logout page, or try manually deleting the cookie - other than that, all i can suggest is ask at webwizguide