Finding Age from Date of Birth

Discussion in 'ASP General' started by Gav, Jan 4, 2004.

  1. Gav

    Gav Guest

    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
     
    Gav, Jan 4, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:DBVJb.916$...
    > 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
    >
    >
     
    Aaron Bertrand [MVP], Jan 4, 2004
    #2
    1. Advertising

  3. Gav

    UncleWobbly Guest

    age = datediff("y",birthdate,now)

    gives it in whole years ("y")


    "Gav" <> wrote in message
    news:DBVJb.916$...
    > 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
    >
    >
     
    UncleWobbly, Jan 4, 2004
    #3
  4. Gav

    Bob Barrows Guest

    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"
     
    Bob Barrows, Jan 4, 2004
    #4
  5. Gav

    Gav Guest

    > 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??
     
    Gav, Jan 4, 2004
    #5
  6. Gav

    Evertjan. Guest

    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)
     
    Evertjan., Jan 4, 2004
    #6
  7. > 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/
     
    Aaron Bertrand [MVP], Jan 4, 2004
    #7
  8. Gav

    Guest 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

    ?

    "Aaron Bertrand [MVP]" <> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:DBVJb.916$...
    > > 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
    > >
    > >

    >
    >
     
    Guest, Jan 4, 2004
    #8
  9. > 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...
     
    Aaron Bertrand [MVP], Jan 4, 2004
    #9
  10. Gav

    Gav Guest

    > 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
     
    Gav, Jan 4, 2004
    #10
  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?
     
    Aaron Bertrand [MVP], Jan 4, 2004
    #11
  12. Gav

    Brynn Guest

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



    On 04 Jan 2004 19:28:33 GMT, "Evertjan."
    <> 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)
     
    Brynn, Jan 4, 2004
    #12
  13. Gav

    Brynn Guest

    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."
    <> 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)
     
    Brynn, Jan 4, 2004
    #13
  14. Gav

    Brynn Guest

    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" <> 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
    >
    >
     
    Brynn, Jan 4, 2004
    #14
  15. Gav

    Guest 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.


    "Aaron Bertrand [MVP]" <> wrote in message
    news:...
    > > 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...
    >
    >
     
    Guest, Jan 4, 2004
    #15
  16. Gav

    dlbjr Guest

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

    -dlbjr

    Discerning resolutions for the alms
     
    dlbjr, Jan 4, 2004
    #16
  17. Gav

    Brynn Guest

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

    On Sun, 4 Jan 2004 17:38:27 -0600, "dlbjr" <> wrote:

    >dblAge = DateDiff("d",dtmBirthDate,Date) / 365.25
    >
    >-dlbjr
    >
    >Discerning resolutions for the alms
    >
    >
     
    Brynn, Jan 5, 2004
    #17
  18. > "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"...
     
    Aaron Bertrand [MVP], Jan 5, 2004
    #18
  19. Gav

    Brynn Guest

    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]"
    <> 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"...
    >
    >
     
    Brynn, Jan 5, 2004
    #19
  20. > 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
     
    Aaron Bertrand [MVP], Jan 5, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TJS
    Replies:
    9
    Views:
    16,202
    gosborne
    Sep 7, 2010
  2. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    830
    Roedy Green
    Jan 2, 2006
  3. cylin
    Replies:
    6
    Views:
    576
    Mike Wahler
    Aug 19, 2003
  4. Rick North

    Birth date for VHDL 87 ?

    Rick North, Jan 19, 2007, in forum: VHDL
    Replies:
    2
    Views:
    427
    Rick North
    Jan 20, 2007
  5. eggie5

    Age to birth year

    eggie5, Sep 25, 2007, in forum: Ruby
    Replies:
    2
    Views:
    102
    Jan Friedrich
    Sep 25, 2007
Loading...

Share This Page