Here is a function for finding age

B

Brynn

Here is a function where you don't have to worry about the leap year.

You will have to still be aware of time differences between you and
the server, and correct your data before entering it into the
function. And of course time zones, yada yada ....

BUT I think that most people using this will just want to say if today
is there bday than they are their new age.



It first takes the currentYear - birthYear - 1

Then it decides whether it needs to add a year(i.e if they had their
bday). First by just making a number out of the month and day ... it
makes the day 2 digits by adding a '0' in front of a single digit day.
Then puts month and day into one number .. like this

feb 2 = 202
july 10 = 710
oct 8 = 1008
dec 20 = 1220

and yes feb 29 = 229

Then if today is 301 it doesn't care ... 228 and 229 are both less
than 301.

I also added a part to compare ONLY the times ... if the day
comparison = 0 ... i.e. today is there bday

now, if you don't send a time with your date ... no problem ... it
will count today as being their new age

Tell me what you think ... I can clean up the code a bit if anyone
wants me to


<%
Function yearsOld(birthDate)
Dim currentDate, monthDayComparison, addYear: currentDate =
Now()

Dim birthDay, currentDay: birthDay = Day(birthDate):
currentDay = Day(currentDate)

'// Lets take the Date() BS out of the picture!!!
'//Compare Days by making a number out of month & day
.... feb 29 = 229 while oct 8 = 1008
If Len(birthDay) = 1 Then: birthDay = "0" & birthDay:
End If
If Len(currentDay) = 1 Then: currentDay = "0" &
currentDay: End If
monthDayComparison = Int(Int(Month(currentDate) &
currentDay)) - Int(Month(birthdate) & birthDay)


If monthDayComparison > 0 Then '//had birthday this
year
addYear = 1
ElseIf monthDayComparison < 0 Then '//haven't had
birthday this year
addYear = 0
ElseIf monthDayComparison = 0 Then '// birthday today
addYear = 1
Dim timeDifference: timeDifference =
DateDiff("s", Hour(currentDate) & ":" & Minute(currentDate) & ":" &
Second(currentDate), Hour(birthDate) & ":" & Minute(birthDate) & ":" &
Second(birthDate))
If timeDifference > 0 Then: addYear = 0: End
If
End If

yearsOld = Year(currentDate) - Year(birthDate) - 1 + addYear
End Function
%>
 
R

Robert Suffecool

You're working to hard! Take a look at the datediff() function in VS6 help
files. I believe that function will save you a lot of time. I've attached
a crude asp example to this post for you to play with.

God Bless,

Robert
 
B

Brynn

I was just playing around more than anything ... they were arguing it
in another thread ... aspfaq.com has a script to identify leap year
babies, but I don't think that it will come up with a different result
than ...

age = Int(DateDiff("d",dob, Date()) / 365.25)

then if someone REALLY wanted to go by the time of birth for whatever
crazy reason ... they could just

addToAge = DateDiff( "s", Hour(dob) & ":" & Minute(dob) & ":" &
Second(dob), Hour(Date()) & ":" & Minute(Date)) & ":" &
Second(Date()))

If addToAge =< 0 Then: age = age + 1: End if

I have yet to find where dividing the difference by 365.25 will not
give the correct response

Thanks for the input though Robert :)

Take Care,

Brynn
www.coolpier.com


You're working to hard! Take a look at the datediff() function in VS6 help
files. I believe that function will save you a lot of time. I've attached
a crude asp example to this post for you to play with.

God Bless,

Robert

Brynn said:
Here is a function where you don't have to worry about the leap year.

You will have to still be aware of time differences between you and
the server, and correct your data before entering it into the
function. And of course time zones, yada yada ....

BUT I think that most people using this will just want to say if today
is there bday than they are their new age.



It first takes the currentYear - birthYear - 1

Then it decides whether it needs to add a year(i.e if they had their
bday). First by just making a number out of the month and day ... it
makes the day 2 digits by adding a '0' in front of a single digit day.
Then puts month and day into one number .. like this

feb 2 = 202
july 10 = 710
oct 8 = 1008
dec 20 = 1220

and yes feb 29 = 229

Then if today is 301 it doesn't care ... 228 and 229 are both less
than 301.

I also added a part to compare ONLY the times ... if the day
comparison = 0 ... i.e. today is there bday

now, if you don't send a time with your date ... no problem ... it
will count today as being their new age

Tell me what you think ... I can clean up the code a bit if anyone
wants me to


<%
Function yearsOld(birthDate)
Dim currentDate, monthDayComparison, addYear: currentDate =
Now()

Dim birthDay, currentDay: birthDay = Day(birthDate):
currentDay = Day(currentDate)

'// Lets take the Date() BS out of the picture!!!
'//Compare Days by making a number out of month & day
... feb 29 = 229 while oct 8 = 1008
If Len(birthDay) = 1 Then: birthDay = "0" & birthDay:
End If
If Len(currentDay) = 1 Then: currentDay = "0" &
currentDay: End If
monthDayComparison = Int(Int(Month(currentDate) &
currentDay)) - Int(Month(birthdate) & birthDay)


If monthDayComparison > 0 Then '//had birthday this
year
addYear = 1
ElseIf monthDayComparison < 0 Then '//haven't had
birthday this year
addYear = 0
ElseIf monthDayComparison = 0 Then '// birthday today
addYear = 1
Dim timeDifference: timeDifference =
DateDiff("s", Hour(currentDate) & ":" & Minute(currentDate) & ":" &
Second(currentDate), Hour(birthDate) & ":" & Minute(birthDate) & ":" &
Second(birthDate))
If timeDifference > 0 Then: addYear = 0: End
If
End If

yearsOld = Year(currentDate) - Year(birthDate) - 1 + addYear
End Function
%>


begin 666 datediff.asp
M/$A434P^#0H\2$5!1#X-"CQ-151!($Y!344](D=%3D52051/4B(@0V]N=&5N
M=#TB36EC<F]S;V9T(%9I<W5A;"!3='5D:6\@-BXP(CX-"CQ4251,13Y(;W<@
M;VQD(&%R92!Y;W4_/"]4251,13X-"CPO2$5!1#X-"CQ"3T19/@T*#0H\)4EF
M(%)E<75E<W0N1F]R;2@B='AT1$]"(BD@/#X@(B(@5&AE;@T*"0T*"5)E<W!O
M;G-E+E=R:71E*")9;W4@87)E("(@)B!R;W5N9"AD871E9&EF9B@B9"(L4F5Q
M=65S="Y&;W)M*")T>'1$3T(B*2QN;W<I+S,V-2PR*2 F("(@>65A<G,@;VQD
M(2(I"0T*"0T*("!E;'-E("4^#0H-"CQF;W)M(&UE=&AO9#TB<&]S="(@86-T
M:6]N/2(O9&%T961I9F8N87-P(CX-"D1/0C\@/$E.4%54(&ED/71X=$1/0B!S
M='EL93TB3$5&5#H@,3!P>#L@5$]0.B Q-G!X(B -"FYA;64]='AT1$]"/@T*
M/$E.4%54(&ED/7-U8FUI=#$@<W1Y;&4](DQ%1E0Z(#(U-G!X.R!43U Z(#$U
M<'@B('1Y<&4]<W5B;6ET('9A;'5E/5-U8FUI="!N86UE/7-U8FUI=#$^/"]F
M;W)M/@T*/"5E;F0@:68E/@T*#0H-"@T*#0H-"@T*#0H\+T)/1%D^#0H\+TA4
%34P^#0H`
`
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top