Finding Age from Date of Birth

G

Gav

I have a database with date of births stored dd/mm/yyyy (english dating
system) and =date() returns a date in the same format in my server.

how do i find the persons age using these two pieces of date.

thanks
gavin
 
B

Bob Barrows

UncleWobbly said:
age = datediff("y",birthdate,now)

gives it in whole years ("y")
No, it doesn't. See Aaron's faq article for the reason.

Bob Barrows
 
E

Evertjan.

Bob Barrows wrote on 04 jan 2004 in
microsoft.public.inetserver.asp.general:
No, it doesn't. See Aaron's faq article for the reason.

It should.

IF
the value of birthdate includes the exact time of birth
AND
the servertime is in the same timezone
as in which the birthdate was specified
AND
the servertime is in the same daylight saving correction
as in which the birthdate is specified
THEN
age would give the correct number of years
BUTFOR
the crazy notion that you get your additional year
at 00:00 local time, independent of the sometimes different
local time of the place of birth
AND
independent of the exact time of birth anyhow
SO
this calls for first getting the definition of "age" right
ANDTHEN
correcting the inputvalues of DateDiff to that definition
BUTMIND
that there can be a conciderable difference in de time of the first
appearence of the head in the case of a "normal" [=normalized?] birth and
the final delivery of the feet q.q. other hinter parts
SO
event the date of birth could be agued to be on different dates
AND
the birth could have happened in an aeroplane
OR
on the international dateline
OR
exactly on one of the poles
OR
on a spaceship
OR
another planet
INWHICHCASE
all odds are off
ENDIF
 
A

Aaron Bertrand [MVP]

thats looks to be cool but how can i input my dd/mm/yyyy figure which is
stored in my db??

Where do you need to input it? And please stop worrying about dd/mm/yyyy.
One of the reasons I suggested doing this in the database is so that you
don't have to worry about the string representation of the date. The date
isn't actually *stored* that way in the database.

So, let's step back. What is "my db"? Access 97, SQL Server 2000, Oracle,
....?
 
G

Guest

The thing described there is quite complecated, even takes leap years into
account.
In practice, when asking a person's age one expect to be given number of
full years.
Cannot this be done by converting the two dates to strings, chopping off dd
and mm, convert the rest yyyy to number and see the difference?
To increase the precision same might be done on mm

?
 
A

Aaron Bertrand [MVP]

Cannot this be done by converting the two dates to strings, chopping off
dd
and mm, convert the rest yyyy to number and see the difference?

Sure, if you want a rough guess. Do you care more about accuracy, or about
tidiness of code? Your code can still be tidy, you throw the logic into a
function...
 
G

Gav

Where do you need to input it? And please stop worrying about dd/mm/yyyy.
One of the reasons I suggested doing this in the database is so that you
don't have to worry about the string representation of the date. The date
isn't actually *stored* that way in the database.

So, let's step back. What is "my db"? Access 97, SQL Server 2000, Oracle,
...?

ok, i have a date stored in a field, i want to transplant this data into
that script so it can return another variable whihc is the date...

my db is access 2000

btw sorry for my non technical language. :(

gav
 
A

Aaron Bertrand [MVP]

ok, i have a date stored in a field, i want to transplant this data into
that script so it can return another variable whihc is the date...

I don't understand your narrative. Could you show a few sample rows from
your database, and what you want presented on the ASP page?
 
B

Brynn

If BUT MIND could only be proper ASP syntax ... that would ROCK



Bob Barrows wrote on 04 jan 2004 in
microsoft.public.inetserver.asp.general:
No, it doesn't. See Aaron's faq article for the reason.

It should.

IF
the value of birthdate includes the exact time of birth
AND
the servertime is in the same timezone
as in which the birthdate was specified
AND
the servertime is in the same daylight saving correction
as in which the birthdate is specified
THEN
age would give the correct number of years
BUTFOR
the crazy notion that you get your additional year
at 00:00 local time, independent of the sometimes different
local time of the place of birth
AND
independent of the exact time of birth anyhow
SO
this calls for first getting the definition of "age" right
ANDTHEN
correcting the inputvalues of DateDiff to that definition
BUTMIND
that there can be a conciderable difference in de time of the first
appearence of the head in the case of a "normal" [=normalized?] birth and
the final delivery of the feet q.q. other hinter parts
SO
event the date of birth could be agued to be on different dates
AND
the birth could have happened in an aeroplane
OR
on the international dateline
OR
exactly on one of the poles
OR
on a spaceship
OR
another planet
INWHICHCASE
all odds are off
ENDIF
 
B

Brynn

I think I am going to write a function to be all exclusive ...
including variables for being born in an airplane on the international
dateline

LOL

Bob Barrows wrote on 04 jan 2004 in
microsoft.public.inetserver.asp.general:
No, it doesn't. See Aaron's faq article for the reason.

It should.

IF
the value of birthdate includes the exact time of birth
AND
the servertime is in the same timezone
as in which the birthdate was specified
AND
the servertime is in the same daylight saving correction
as in which the birthdate is specified
THEN
age would give the correct number of years
BUTFOR
the crazy notion that you get your additional year
at 00:00 local time, independent of the sometimes different
local time of the place of birth
AND
independent of the exact time of birth anyhow
SO
this calls for first getting the definition of "age" right
ANDTHEN
correcting the inputvalues of DateDiff to that definition
BUTMIND
that there can be a conciderable difference in de time of the first
appearence of the head in the case of a "normal" [=normalized?] birth and
the final delivery of the feet q.q. other hinter parts
SO
event the date of birth could be agued to be on different dates
AND
the birth could have happened in an aeroplane
OR
on the international dateline
OR
exactly on one of the poles
OR
on a spaceship
OR
another planet
INWHICHCASE
all odds are off
ENDIF
 
B

Brynn

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

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

Guest

"Do you care more about accuracy, or about tidiness of code?"

About optimal ratio between these two.
Precision has to fit the purpose.
Gav is asking about people's age, and in this case full years (or may be
full months) should suffice, unless he is doing a new baby database for a
maternity unit - then one would need not only days but hours as well.
 
D

dlbjr

dblAge = DateDiff("d",dtmBirthDate,Date) / 365.25

-dlbjr

Discerning resolutions for the alms
 
A

Aaron Bertrand [MVP]

"Do you care more about accuracy, or about tidiness of code?"
About optimal ratio between these two.
Precision has to fit the purpose.
Gav is asking about people's age, and in this case full years (or may be
full months) should suffice, unless he is doing a new baby database for a
maternity unit - then one would need not only days but hours as well.

So if you want to know if today is someone's birthday, you just need to know
that it's in the current month?

And if you want to remind someone that their friend's birthday is one week
away, it doesn't matter when you do it, as long as it is within the current
month?

If you want to know the birth MONTH, ask for that. If you want to know the
birth DAY, well, that requires accuracy to the day, not the month.

Again, the code can be as tidy as you want it, because you can stuff the
logic away in a function. Encapsulation is a pretty fundamental concept of
software engineering... and so is being accurate as opposed to "close
enough"...
 
B

Brynn

I certainly am not trying to argue a point ... just curious ...
what dates will give a different response from the subroutine on
aspfaqs then the following ...

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

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

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

I am only asking to see if I am missing something. Not to be a
smart-arse ... my name is Brynn not Bob :)

Thanks in advance for your reply,

Brynn
 
A

Aaron Bertrand [MVP]

I certainly am not trying to argue a point ... just curious ...
what dates will give a different response from the subroutine on
aspfaqs then the following ...

I have no idea, I haven't tested it. I imagine there might be a problem for
leap year babies at the century that does NOT have a leap year (something
about divisible by 4, but not 400)?

A
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top