473,378 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Finding Age from Date of Birth

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
Jul 19 '05 #1
20 11769
You should perform the calculation in the database (the database knows the
current date also).

Here are both VBScript and database methods:
http://www.aspfaq.com/2233

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/


"Gav" <ga*****@ntlworld.com> wrote in message
news:DB***************@newsfep1-gui.server.ntli.net...
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

Jul 19 '05 #2
age = datediff("y",birthdate,now)

gives it in whole years ("y")
"Gav" <ga*****@ntlworld.com> wrote in message
news:DB***************@newsfep1-gui.server.ntli.net...
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

Jul 19 '05 #3
UncleWobbly wrote:
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
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #4
Bob Barrows wrote on 04 jan 2004 in
microsoft.public.inetserver.asp.general:
UncleWobbly wrote:
age = datediff("y",birthdate,now)

gives it in whole years ("y")

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 19 '05 #5
Gav
> Here are both VBScript and database methods:
http://www.aspfaq.com/2233


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


Jul 19 '05 #6
> 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,
....?

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
Jul 19 '05 #7
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

?

"Aaron Bertrand [MVP]" <aa***@TRASHaspfaq.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
You should perform the calculation in the database (the database knows the
current date also).

Here are both VBScript and database methods:
http://www.aspfaq.com/2233

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/


"Gav" <ga*****@ntlworld.com> wrote in message
news:DB***************@newsfep1-gui.server.ntli.net...
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


Jul 19 '05 #8
> 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...
Jul 19 '05 #9
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
Jul 19 '05 #10

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

On 04 Jan 2004 19:28:33 GMT, "Evertjan."
<ex**************@interxnl.net> wrote:
Bob Barrows wrote on 04 jan 2004 in
microsoft.public.inetserver.asp.general:
UncleWobbly wrote:
age = datediff("y",birthdate,now)

gives it in whole years ("y")

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Jul 19 '05 #11
> 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?
Jul 19 '05 #12

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

On 04 Jan 2004 19:28:33 GMT, "Evertjan."
<ex**************@interxnl.net> wrote:
Bob Barrows wrote on 04 jan 2004 in
microsoft.public.inetserver.asp.general:
UncleWobbly wrote:
age = datediff("y",birthdate,now)

gives it in whole years ("y")

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

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Jul 19 '05 #13
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
%>

On Sun, 4 Jan 2004 14:40:14 -0000, "Gav" <ga*****@ntlworld.com> wrote:
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


Jul 19 '05 #14
"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.
"Aaron Bertrand [MVP]" <aa***@TRASHaspfaq.com> wrote in message
news:Oi**************@tk2msftngp13.phx.gbl...
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...

Jul 19 '05 #15
dblAge = DateDiff("d",dtmBirthDate,Date) / 365.25

-dlbjr

Discerning resolutions for the alms
Jul 19 '05 #16

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

On Sun, 4 Jan 2004 17:38:27 -0600, "dlbjr" <do******@do.u> wrote:
dblAge = DateDiff("d",dtmBirthDate,Date) / 365.25

-dlbjr

Discerning resolutions for the alms


Jul 19 '05 #17
> "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"...
Jul 19 '05 #18

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

On Sun, 4 Jan 2004 21:59:35 -0500, "Aaron Bertrand [MVP]"
<aa***@TRASHaspfaq.com> wrote:
"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"...


Jul 19 '05 #19
> 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
Jul 19 '05 #20
"So if you want to know if today is someone's birthday"

If I want to know if today is someone's birthday I will write a query to do
just this job.
Something like
WHERE dd/mm of DOB = dd/mm of TODAY

I am not argue about the correctness of your solution.
I am just trying to see what is better - one big universal tool or several
dedicated small tools
"Aaron Bertrand [MVP]" <aa***@TRASHaspfaq.com> wrote in message
news:O$**************@TK2MSFTNGP12.phx.gbl...
"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"...

Jul 19 '05 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: KL | last post by:
I am working on a problem and desperately need help! I need to prompt a user for the numerical month of birth, day of birth and year of birth and store it in varialbes and the use the variables...
2
by: Robizzle | last post by:
Sorry, I must be blind but I can't seem to find it anywhere. What function will return the time a file was created? I see filemtime(...) returns the time that file was last Modified and...
4
stormrider
by: stormrider | last post by:
Hi all, I'm trying to implement a Perl structure. My small program will do the followings; Take the birth date of the user as input. (Month's will be input as strings.) Take the current time...
3
by: jamieharrop | last post by:
Afternoon all, I've been battling with this all day today and my brain is now pretty much fried. I have one table that lists several details about my customers (name, address, phone, date of...
12
by: lolodede | last post by:
i like to validate date of birth as format dd/mm/yyy i tried everything but is not working thank you
1
by: andsonlhs | last post by:
Not allow the user to register future date,if register future date and then it will alert invalid date.Please Help..... How to set validation date in javacript??? <table width="100%" border=0...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.