Best Way to Calculate Age

  • Hugo> "...(a daunting task by now, I know )..."

    lol, one of the reasons I love this site. We can all hash out ideas without egos getting in the way.

  • But even your own "timeanddate.com" link makes it clear that the official birthdate is Mar 1 in non-leap years:

    while others celebrate their birthday on March 1 because they do not officially turn next age on February 28.

    [emphasis added]

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • The people you know who celebrate their birthdays on Feb 28 on non-leap years, doesn't mean the state or country they live in says they are another year older yet. The state or country may not recognize that they are another year older until March 1st. If you look at how many days into the year they are born (February 29th is the 60th day of the year), it makes sense that March 1st (the 60th day of the year in non leap years) is the point in time that they are another year older.

    Before you take this the other way, yes everyone born from March 1st onward could consider themselves a year older a day earlier during a leap year, but I don't think any of us old timers would care to do that.

    If you are born on February 29th, celebrate your birthday on February 28 or on March 1 (heck, celebrate it on both days), but I wouldn't say I was a year older until March 1st. Personal opinion, your opinion may differ and that's fine.

    😎

  • ScottPletcher (3/10/2008)


    But even your own "timeanddate.com" link makes it clear that the official birthdate is Mar 1 in non-leap years:

    while others celebrate their birthday on March 1 because they do not officially turn next age on February 28.

    [emphasis added]

    You may have missed the part in the very next paragraph that says that the rules vary depending on where they live.

    "In some cases, their legal birthdays depend on the rules and regulations of where they live. Many countries make amendments for those born on leap days so they can be considered eligible for marriage, driving and other activities that require a legal age."

    Some officially age a year on the 1st, some on the 28th, depending on where they live. I'm not saying that my function takes that into account, because it obviously doesn't. I'm instead saying none of the functions in this thread take locality into account. That makes my assumption (Feb 28th), no more and no less valid than the ones that assume otherwise (Mar 1st).

    To truly build a function that would take all combinations of dates into account, it would have to account for prior calendars, as well as for Leap Year, and possibly even for differences in time zone (if you need that kind of precision).

    For example of how complex this simple question can get, if someone was born on 28 Feb in the year 5 BC, how old was he on 28 Feb 5 AD? Or, for a sort of trick question, if someone was born on 13 July 100 BC, and died on 15 March in 44 BC, how old was he? (Those are the dates given in Wikipedia for Julius Caesar's life span. But the month of "July" didn't exist until later, since it was named after him.) None of the functions given (including mine) can deal with either of those questions, since SQL Server's datetime data type doesn't correctly deal with BC dates and pre-Julian, Roman calendars.

    Also, none of the examples I'm seeing except mine take into account time on the birth certificate. This can matter in the case of, for example, a statuatory rape trial. Was the alleged victim 15 at the exact time of the alleged crime, or 16? It can make a difference.

    It all depends on the proposed purpose of the function. In the case of this particular Question of the Day, it was "which of these is the best", and there really should have been a "none of the above" option, since all three given options were wrong.

    In summary, NONE of the functions outlined in this thread yet are correct in all possible cases. Some of them are MORE correct than the answers given as options.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Lynn Pettis (3/10/2008)


    The people you know who celebrate their birthdays on Feb 28 on non-leap years, doesn't mean the state or country they live in says they are another year older yet. The state or country may not recognize that they are another year older until March 1st. If you look at how many days into the year they are born (February 29th is the 60th day of the year), it makes sense that March 1st (the 60th day of the year in non leap years) is the point in time that they are another year older.

    Before you take this the other way, yes everyone born from March 1st onward could consider themselves a year older a day earlier during a leap year, but I don't think any of us old timers would care to do that.

    If you are born on February 29th, celebrate your birthday on February 28 or on March 1 (heck, celebrate it on both days), but I wouldn't say I was a year older until March 1st. Personal opinion, your opinion may differ and that's fine.

    😎

    Totally correct. They may or may not have been "officially" older. In fact, it's quite possible they may have been both. I don't know the laws for Los Angeles, CA, regarding this, but neither do I know the laws of Sweden (one of the two was Swedish, or maybe Swiss, it's been 20 years and I know it was one of the two), nor of Seattle (the other one). If, for example, Swedish law says, "you're a year older on Feb 29 or Mar 1, whichever occurs first", and California law says, "on non-Leap Years, your birthday is official Feb 28", her passport might give her one age while her California ID card might have another. I don't know.

    That's the fun part of a question like this. It's like Phil Factor's http://www.simple-talk.com/opinion/opinion-pieces/the-joy-of-nad/ about the simplicity of a table for Name and Address. Starts out as something that should be solveable with simple common sense, and then explodes into a monstrous complexity that, all by itself, probably raises the stock prices for the companies that produce headache medication.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • I don't know the laws for Los Angeles, CA, regarding this ... California law says, "on non-Leap Years, your birthday is official Feb 28"

    Wouldn't L.A. fall under CA law?

    Where did you get that statement of CA law?

    All I can find for America says you're not legally older until March 1. (Although I certainly haven't found things for all states / territories, etc..)

    Feb 28 is just so impractical, because specific checks will have to be done for that. Moreover, no one else's birthday is ever before their birthday ... that's just too odd for me.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • GSquared (3/10/2008)


    Lynn and Hugo:

    I took Leap Years into account, and even (to be totally anal retentive about it), the hour, minute, second, and number of milliseconds, on each time.

    Maybe I'm operating on the wrong standard here, but the people I have known who had Feb 29 birthdays (2 people, admitedly a very small sample), both celebrated their birthday on Feb 28 on non-leap years, not on March 1.

    Hi GSquared,

    I'll gladly admit that I personally know no leaplings and that I had not bothered to look up any officiall info before. I just went by my personal common sense, which told me that one can not be considered a year older until MMDD of the current date is equal to or greater than MMDD of the birthdate.

    I also followed the link you posted. I found that site to be quite confusing, so I then went to Wikipedia (http://en.wikipedia.org/wiki/Leap_year#Birthdays). There, I found a clear description of at least one country (Taiwan) where leaplings are officially considered to be a year older on Feb 28 in common years. I didn't read on, since one counterexample always suffices to disprove any axiom.

    Since apparently the rules for leaplings differ per country (and apparently even per state in some countries), I can only agree that there is no single universally correct formula.

    As far as I see, your formula will indeed return correct results for all date combinations if the Taiwanese rules for leaplings apply. 😉


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • ScottPletcher (3/10/2008)


    I don't know the laws for Los Angeles, CA, regarding this ... California law says, "on non-Leap Years, your birthday is official Feb 28"

    Wouldn't L.A. fall under CA law?

    Where did you get that statement of CA law?

    All I can find for America says you're not legally older until March 1. (Although I certainly haven't found things for all states / territories, etc..)

    Feb 28 is just so impractical, because specific checks will have to be done for that. Moreover, no one else's birthday is ever before their birthday ... that's just too odd for me.

    You may have misunderstood me. I was giving a "for example". I have no idea what California law is on the subject. That was the whole point of that paragraph. (Edit: you elipsed out the part of my writing where I said, "what if, for example...", which pretty clearly states that I'm not quoting some statute on the subject.)

    Per the web page I originally referenced, South Carolina and Maryland have different policies/problems with 29 Feb birthdates, so from that, I'm extrapolating that the US as a whole does not have a single cohesive policy/law/standard on the subject.

    On the subject of a person's birthday being before their birthday, why does it make more sense for the birthday to be after their birthday than it does to have it be before? Both arguments are forced by the fact that we're stuck using a Roman calendar that tries desperately and oddly to divide a 365.25-day year evenly into 12 months. Of course it has inconsistencies and oddities to it. It would be stranger still if it didn't.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • why does it make more sense for the birthday to be after their birthday than it does to have it be before?

    Because then the exact same check that works for everyone else would work for them. No additional coding/checks required. Otherwise a large number of specific Feb 29/28 checks must be coded into age check ... yuck.

    And I quite agree with Hugo: my common sense tells me the birthday cannot occur on a date before it occurs.

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

  • Hi Scott & GSquared,

    My common sense was actually not about when a birthdate is (I have at times celebrated by birthday several days before or after the day I was born + x years), but at what moment one can be considered to be of a certain age.

    If someone is born in, for instance, 1992, on, let's say, March 11th - then nobody will disagree that his or her age in 2009 is 16 years on all days before March 11th, and 17 years on all days on or after March 11th. Likewise, if this person were born on February 28th, 1992, his or her age in 2009 would be 16 if month/day is less than February 28, and 17 if it's equal to or greater than February 28th.

    So logically, I would expect to be able to apply the same logic to someone born on February 29th 1992 - that in 20009, he or she will be 16 for as long as month/day is less than February 29th, and 17 as soon as month/day is equal to or greater than February 29th. The fact that the "equal to" in this comparison won't happen in 2009 should be immaterial to the logic.

    Obviously, at least the official Taiwan laws disagree with me. So I can't claim this reasoning to be "the truth". But I do still maintain that it IS the most logical way to calculate ages for leaplings, no matter what Taiwan thinks 😉


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Its not only Taiwan, England and Wales also puts the legal birthday of leaplings as Feb 28. I think it has to do with trying to keep the birthday in February.

  • ScottPletcher (3/11/2008)


    why does it make more sense for the birthday to be after their birthday than it does to have it be before?

    Because then the exact same check that works for everyone else would work for them. No additional coding/checks required. Otherwise a large number of specific Feb 29/28 checks must be coded into age check ... yuck.

    And I quite agree with Hugo: my common sense tells me the birthday cannot occur on a date before it occurs.

    Amusingly enough, my function was being critiqued for NOT having special clauses for Feb 29 birthdays. The code I used does not require special handlings for them. It uses native SQL functions (datediff and dateadd) on datetime data types.

    In order to get the results you want, 1 March instead of 28 Feb in non-leap years, you actually have to add more code to the solution.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • All of them are incorrect. I think the correct age will be:

    SELECT DATEDIFF(yy, DateOfBirth, GETDATE()) - CASE WHEN

    DATEPART(m, DateOfBirth) < DATEPART(m, GETDATE()) OR (DATEPART(m, DateOfBirth) = DATEPART(m, GETDATE())

    AND DATEPART(d, DateOfBirth) > DATEPART(d, GETDATE())) THEN 1 ELSE 0 END

  • The "real point" was that the author should have tested before posting. The topic was already full of "solutions" (many of them flawed), so why would I beat a dead horse?

  • lol, as I see I am not the only one angry at the official answer....

    the following 2 statements both return 27 years old...

    select DATEDIFF(yy, '1/1/1980', GETDATE()) - CASE WHEN DATEPART(m, '1/1/1980') >= DATEPART(m, GETDATE()) AND DATEPART(d, '1/1/1980') >= DATEPART(d, GETDATE()) THEN 0 ELSE 1 END

    select DATEDIFF(yy, '11/11/1980', GETDATE()) - CASE WHEN DATEPART(m, '11/11/1980') >= DATEPART(m, GETDATE()) AND DATEPART(d, '11/11/1980') >= DATEPART(d, GETDATE()) THEN 0 ELSE 1 END

    I want my point back!

Viewing 15 posts - 151 through 165 (of 189 total)

You must be logged in to reply to this topic. Login to reply