Results 1 to 38 of 38

Thread: finding someone's age

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586

    finding someone's age

    Greetings,

    If someone's date of birth is March 5, 1982, and I need to find out how old they were as of February 28, 2006, what fucntion could I use.

    When I use

    client_age:datediff("yyyy", [DOB], "2/28/06") ,

    the age result is a year bigger than it should be.

    Tried a couple other differnt ways; but it doesn't like the syntax. Please help!

    Thanks!

    Jim

  2. #2
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: finding someone's age

    If all you are interested in the number of years ("I am 29...again") then try:
    VB Code:
    1. '<CSCM>
    2. '---------------------------------------------------------------------
    3. '-- Name:   GetPersonsAge
    4. '--   By:   <SGD> on 04/27/2006
    5. '-- Desc:   Returns the exact age of a person in years based on birthday, and
    6. '--     an optional second date (default value is NOW)
    7. '---------------------------------------------------------------------
    8. '-- Note:   DateDiff does not work properly for this.
    9. '---------------------------------------------------------------------
    10. '-- Revd:   <SGD> - 05/26/2006
    11. '---------------------------------------------------------------------
    12. '-- Parm:   BirthDate (Variant)
    13. '           LaterDate (Variant)
    14. '---------------------------------------------------------------------
    15. '</CSCM>
    16. Public Function GetPersonsAge(BirthDate As Variant, _
    17.                               Optional LaterDate As Variant) As Long
    18.    
    19.     Dim dteLaterDate As Date
    20.     If IsDate(BirthDate) Then
    21.         If Not (IsDate(LaterDate)) Then
    22.             dteLaterDate = CVDate(Now)
    23.         Else
    24.             dteLaterDate = LaterDate
    25.         End If
    26.         '---------------------------------------------------------------------
    27.         GetPersonsAge = Year(dteLaterDate) - Year(BirthDate)
    28.         '---------------------------------------------------------------------
    29.         '-- Subtract one if this year's birthday hasn't occurred yet
    30.         '---------------------------------------------------------------------
    31.         If DateSerial(Year(dteLaterDate), Month(BirthDate), Day(BirthDate)) > dteLaterDate Then
    32.             GetPersonsAge = GetPersonsAge - 1
    33.         End If
    34.     End If
    35. End Function
    36. '*********************************************************************
    37. '-- EXAMPLE CODE
    38. '*********************************************************************
    39. '-- Assume that today is 5/8/2006
    40. 'Debug.Print GetPersonsAge("05/31/1950")                    '-- Prints 55 which is correct
    41. 'Debug.Print DateDiff("yyyy", "05/31/1950", "05/08/2006")   '-- Prints 56 which is incorrect
    42. 'Debug.Print GetPersonsAge("05/32/1950")                    '-- Prints 0 which is correct

  3. #3
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: finding someone's age

    Quote Originally Posted by JimMuglia
    Greetings,

    If someone's date of birth is March 5, 1982, and I need to find out how old they were as of February 28, 2006, what fucntion could I use.

    When I use

    client_age:datediff("yyyy", [DOB], "2/28/06") ,

    the age result is a year bigger than it should be.

    Tried a couple other differnt ways; but it doesn't like the syntax. Please help!

    Thanks!

    Jim
    are you sure??

    i tried this:
    VB Code:
    1. DateDiff("yyyy", "3/5/1982", "2/28/2006")
    and it worked fine. showed 24
    Show Appreciation. Rate Posts.

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: finding someone's age

    ?? datediff("yyyy", #3/5/1982#, #2/28/2006#) = 24 which is correct

    change your 2/28 to a date... and try cdate([dob])
    VB Code:
    1. datediff("yyyy", CDate([DOB]), #2/28/2006#)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: finding someone's age

    ...I must be missing something: If I was born in 1982 but my birthday in 2006 has NOT come yet, then I am 23, not 24....???

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: finding someone's age

    if your birthday has NOT come up yet.. true.. BUT
    if u are comparing it to 2/28/2006 then yes.. u are 24
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: finding someone's age

    Quote Originally Posted by sigid
    ...I must be missing something: If I was born in 1982 but my birthday in 2006 has NOT come yet, then I am 23, not 24....???
    in that case, try sigid example.
    Show Appreciation. Rate Posts.

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: finding someone's age

    oops.. its rounding up!!

    d = DateDiff("d", #8/22/1986#, Date)
    MsgBox d / 365
    19.8

    DateDiff("yyyy", #8/22/1986#, Date)
    20
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: finding someone's age

    Static -
    - I agree with everything you said....
    If I was born on March 5, 1982 and today is 2/28/2006 (not yet March 5, 2006) then...23...

  10. #10
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: finding someone's age

    heres a one liner! LOL

    VB Code:
    1. Left(CStr(DateDiff("d", #8/22/1986#, Date) / 365), InStr(CStr(DateDiff("d", #8/22/1986#, Date) / 365), ".") - 1)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  11. #11
    Addicted Member
    Join Date
    Apr 2006
    Location
    Sweden
    Posts
    173

    Re: finding someone's age

    this one is much better:

    VB Code:
    1. Dim theage As String
    2. theage = InputBox("How old are you?")

    lol...

    Are you with me folks!
    Please Help Us To Save Ana

    You never fail before you stop trying!
    ______________________________
    If I manage to say something good...please reputate
    ______________________________
    ________

    Links:
    Intellithing
    Digitala.nu
    Norrköpings Goklubb
    WoW Trade Center

  12. #12
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: finding someone's age

    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  13. #13
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: finding someone's age

    Try this one out for size:
    ? Left(CStr(DateDiff("d", #6/9/1986#, Date) / 365), InStr(CStr(DateDiff("d", #6/9/1986#, Date) / 365), ".") - 1)

    Since today is 6/8/06, and the above yields 20, instead of 19...
    (also yields 20 for 6/10/1986, 6/11/1986...why am I such a nitpicker???)

  14. #14
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: finding someone's age

    oops.. leap years..
    Left(CStr(DateDiff("d", #6/9/1986#, Date) / 365.25), InStr(CStr(DateDiff("d", #6/9/1986#, Date) / 365.25), ".") - 1)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  15. #15
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: finding someone's age

    Blah.. ok.. so it wasnt perfect.. If u put todays date it to breaks oh well.. gave it a shot LOL
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  16. #16
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: finding someone's age

    Wait!! one more shot.. I think I got it!

    One line
    oh.. taking input from text1 ....
    VB Code:
    1. Left(CStr(DateDiff("d", CDate(Text1), Date) / 365.25), _
    2.             IIf(InStr(CStr(DateDiff("d", CDate(Text1), Date) / 365.25), ".") <> 0, _
    3.             InStr(CStr(DateDiff("d", CDate(Text1), Date) / 365.25), ".") - 1, _
    4.             Len(CStr(DateDiff("d", CDate(Text1), Date) / 365.25))))
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  17. #17
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: finding someone's age

    Sorry, but when you hardcode 365.25, the starting date becomes critical.

    Try the above for 6/8/1988 and you'll get 17...

    (and some time real soon now, I am definitely going to get a real life...)

  18. #18
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: finding someone's age


    bah...

    17.9986310746064
    so close! thats it.. I give up...
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  19. #19
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: finding someone's age

    (Don't forget the International Date Line)

  20. #20
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding someone's age

    i haven't tested it, but try this on for size:
    VB Code:
    1. Dim lAge As Long
    2.     lAge = DateDiff("yyyy", #8/22/1986#, Now)
    3.     If CDate(Format$(Now, "mm/dd")) < CDate(Format$(#8/22/1986#, "mm/dd")) Then lAge = lAge - 1
    4.     Debug.Print lAge

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586

    Re: finding someone's age

    I should have posted this in the database section, as I need to put it for a value in the Access query wizard. I tried the left command above, and its giving me an "invalid procedure call".

    Jim

  22. #22
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: finding someone's age

    OOOHH in access.. ok
    use Sigid's code (towards the top)

    drop the function into a module then use it in the field

    AGE:GetPersonsAge([DOB],#2/28/2006#)

    should work fine
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  23. #23
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding someone's age

    here's mine in functional form:
    VB Code:
    1. Private Function GetAge(ByVal dDOB As Date, ByVal dDate As sdate) As Long
    2.     GetAge = DateDiff("yyyy", dDOB, dDate)
    3.     If CDate(Format$(dDate, "mm/dd")) < CDate(Format$(dDOB, "mm/dd")) Then GetAge = GetAge - 1
    4. End Function
    that'll work fine in VBA (i think)

  24. #24
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: finding someone's age

    yes it will

    although be forewarned.. it will slow down your query to call functions like that
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  25. #25
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: finding someone's age

    how bout this?
    VB Code:
    1. Dim dtBirth As Date, dtNow As Date, dtVar As Date
    2. Dim lngAge As Long
    3.  
    4. dtNow = Date
    5. dtBirth = "6/10/2000"
    6. lngAge = DateDiff("yyyy", dtBirth, dtNow)
    7. dtVar = DateAdd("YYYY", lngAge, dtBirth)
    8. If dtVar > dtNow Then
    9.     lngAge = lngAge - 1
    10. End If
    11. MsgBox lngAge
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  26. #26
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding someone's age

    I know this thread is a couple of days old now, but i thought of a better way to do it:
    VB Code:
    1. Private Function GetAge(ByVal dDOB As Date) As Long
    2.     GetAge = Year(Now - dDOB + 2) - 1900
    3. End Function

  27. #27
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: finding someone's age

    On the person's birthday it should advance a year. IOW, if you were born on 6/12/2000, you're 6 today. So the algorithm should be GetAge = Year(Now - dDOB + 1) - 1900, or it'll be off by one day. (Unless age is figured differently in the UK?)
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  28. #28
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding someone's age

    i didn't really test it - you're probably right. code above ammended.

    Edit: I think it needs to be +2 actually.

  29. #29
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: finding someone's age

    Wow - I cannot believe this thread...

    Calc'ing AGE is a standard formula - not really open for debate!

    But not on this forum

    [edit] I thought this would blow up in VB with Feb 29...

    dtVar = DateAdd("YYYY", lngAge, dtBirth)

    but it only blows up with DATEADD in T-SQL
    Last edited by szlamany; Jun 12th, 2006 at 12:27 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  30. #30
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: finding someone's age

    Quote Originally Posted by bushmobile
    i didn't really test it - you're probably right. code above ammended.

    Edit: I think it needs to be +2 actually.
    For a birthday of tomorrow (mine, actually), 1 makes me a year older from tomorrow on. 2 makes me a year older today.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  31. #31
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding someone's age

    well for the example you gave it needs to be +2. ahh well, s*d it!

  32. #32
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Re: finding someone's age

    Give your music collection a whole new life with PartyTime Jukebox

  33. #33
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: finding someone's age

    Doing division and math tricks to determine an age is a dangerous path to take. It might look neat - but it's simply not a proper formula for age.

    Calculating an age has to be one of the simplest logic-flow concepts to understand. It has one PROCESS step and one CONDITION step...

    1) PROCESS: Subtract Birth year from Current Year
    2) CONDITION: Is Current MM/DD greater than Birth MM/DD
    3) NO: Subtract 1
    4) YES: Do nothing

    Result...Age

    You can do this without using DATEDIFF and DATEADD - it's the easiest of math.

    It works in VB - it works in SQL - it works in any language.

    It handles leap years - not through any effort, but simply because the logic flow mimics the real life calculation that one would do in their own head to determine their own age.

    No one is dividing by 365.25 in their head

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  34. #34
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding someone's age

    no-one is saying that doesn't work, szlamany - it was the very first reply. i think people are just trying to think of different ways.

  35. #35
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: finding someone's age

    My concern is that those other techniques are ridden with possibilies for bugs - division, rounding, variable datatypes (float/integer).

    I'll keep quiet now

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  36. #36
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: finding someone's age

    But how the get axact today age in year Month Day format.

  37. #37
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: finding someone's age

    Quote Originally Posted by shakti5385
    But how the get axact today age in year Month Day format.
    what do you mean?


    and if anyone's bothered to read down this far, here's the age calculation without an if:
    Code:
    Private Function GetAge(ByVal DOB As Date) As Long
        GetAge = DateDiff("yyyy", DOB, Date) + (Date < DateSerial(Year(Date), Month(DOB), Day(DOB)))
    End Function

  38. #38
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: finding someone's age

    I just found this (again!) and I like bushmobile's formula better than my own...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width