views:

3058

answers:

9

I want to calculate the age of a person given the date of birth and the current date in years, months and days relative to the current date.

For example:

>>> calculate_age(2008, 01, 01)
1 years, 0 months, 16 days

Any pointer to an algorithm that does that will be appreciated.

A: 

Since you are obviously using python: Create to datetime objects, one with "birthday" and one with "now", subtract them and read the age from the resulting timedelta object.

Why bothering with things like leap years?

Martin
I use Python, but I am really looking for a generic algorithm that can be implemented in whatever language I use.
Baishampayan Ghose
+3  A: 

That is not an easy question, since above days (if we don't take into account leap-seconds) there are not easy formulas.

Months can consist of 28, 29, 30, or 31 days; years can be 365 or 366 days. Therefore, problems arise when you try to calculate full units of time for months and years.

Here is an interesting article that takes into account all the complex aspects to solve your question in Java.

Fernando Miguélez
It's not just "above hours". Days and weeks are still unambiguous. The real trick is defining the length of a year (and a month, being a 12th of a year, also depends on that definition). But everything else is defined unambiguously in terms of seconds. See my answer below.
dreeves
I updated it from "hours" to "days", you are correct. I do not mention weeks, beacuse it is not usually a desirable way of measuring periods (and in the example ghoseb doesn't refer to weeks specifically either)
Fernando Miguélez
+2  A: 

There is a discussion on python-list about why calculating an age is not so straightforward a task.

From that discussion I found this implementation (which I won't copy and paste here because it's not mine).

I haven't turned up any drop in libraries though.

Aaron Maenpaa
A: 

Here's a way to do it if you're willing to violate the principle of "I should be an integer number of years old on my birthday". Note that that principle isn't strictly true, due to leap years, so the following is actually more accurate, though perhaps less intuitive. If you want to know the actual exact number of years old someone is, this is the way I would do it.

First convert both the birthdate and the current time to epoch time (number of seconds since the dawn of time, ie, 1970 in unix land) and subtract them. See, eg, this question for how to do that: http://stackoverflow.com/questions/95492/how-do-i-convert-a-date. You now have the person's exact age in seconds and need to convert that to a string like "36 years, 3 months, 8 days".

Here's pseudocode to do it. It should be straightforward to remove the weeks or hours or whatever parts you don't want...

daysInYear = 365.2425;  # Average lengh of a year in the gregorian calendar.
                        # Another candidate for len of a year is a sidereal year,
                        # 365.2564 days.  cf. http://en.wikipedia.org/wiki/Year
weeksInYear = daysInYear / 7;
daysInMonth = daysInYear / 12;
weeksInMonth = daysInMonth / 7;
sS = 1;
mS = 60*sS;
hS = 60*mS;
dS = 24*hS;
wS = 7*dS;
oS = daysInMonth*dS;
yS = daysInYear*dS;

# Convert a number of seconds to years,months,weeks,days,hrs,mins,secs.
seconds2str[secs] 
{
  local variables:
    y, yQ= False, o, oQ= False, w, wQ= False,
    d, dQ= False, h, hQ= False, m, mQ= False, s= secs;

  if(secs<0) return "-" + seconds2str(-secs);  # "+" is string concatenation.

  y = floor(s/yS);
  if(y>0) yQ = oQ = wQ = dQ = hQ = mQ = True;
  s -= y * yS;

  o = floor(s/oS);
  if(o>0) oQ = wQ = dQ = hQ = mQ = True;
  s -= o * oS;

  w = floor(s/wS);
  if(w>0) wQ = dQ = hQ = mQ = True;
  s -= w * wS;

  d = floor(s/dS);
  if(d>0) dQ = hQ = mQ = True;
  s -= d * dS;

  h = floor(s/hS);
  if(h>0) hQ = mQ = True;
  s -= h * hS;

  m = floor(s/mS);
  if(m>0) mQ = True;
  s -= m * mS;

  return
    (yQ ? y + " year"  + maybeS(y) + " " : "") + 
    (oQ ? o + " month" + maybeS(o) + " " : "") + 
    (wQ ? w + " week"  + maybeS(w) + " " : "") + 
    (dQ ? d + " day"   + maybeS(d) + " " : "") + 
    (hQ ? dd(h) + ":" : "") + 
    (mQ ? dd(m) + ":" + dd(round(s)) + "s" : s + "s");
}


# Returns an "s" if n!=1 and "" otherwise.
maybeS(n) { return (n==1 ? "" : "s"); }

# Double-digit: takes a number from 0-99 and returns it as a 2-character string.
dd(n) { return (n<10 ? "0" + tostring(n) : tostring(n)); }
dreeves
Unfortunately, without really taking into account leap years and daylight savings time, the results will frequently be a day off.
Michael Borgwardt
Thanks Michael! I prepended a caveat about this to the answer. You could argue that the answer that gives an integer number of years on the person's birthday is what will frequently be a day off! So I think this answer has value too, depending on the exact application.
dreeves
+1  A: 

Since years, months and even days can have uneven lengths, you can't start by subtracting the two dates to get a simple duration. If you want the result to match up with how human beings treat dates, you instead have to work with real dates, using your language's built in functions that understand dates better than you ever will.

How that algorithm is written depends on the language you use, because each language has a different set of functions. My own implementation in Java, using the awkward but technically correct GregorianCalendar:

Term difference (Date from, Date to) {
    GregorianCalendar cal1 = new GregorianCalendar();
    cal1.setTimeInMillis(from.getTime());

    GregorianCalendar cal2 = new GregorianCalendar();
    cal2.setTimeInMillis(to.getTime());

    int years = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR);
    int months = cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);
    int days = cal2.get(Calendar.DAY_OF_MONTH) - cal1.get(Calendar.DAY_OF_MONTH);
    if (days < 0) {
        months--;
        days += cal1.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    if (months < 0) {
        years--;
        months += 12;
    }
    return new Term(years, months, days);
}

This may not be perfect, but it delivers human-readable results. Term is a class of my own for storing human-style periods, since Java's date libraries don't have one.

For future projects I plan to move to the much better Joda date/time library, which does have a Period class in it, and will probably have to rewrite this function.

Marcus Downing
+2  A: 

Others here have the right idea - you simply need be able to extend it to other languages. Many computer systems count time in seconds from a specific point (the "epoch" or "reference date") and figure out the date from there, as well as offering methods to convert from seconds to dates. You should be able to get the current time in seconds, convert the birth date to seconds from the epoch, subtract the two values, then divide that by the number of seconds in a day:

int timenow = time(0);
struct tm birthday = { tm_mday = 16 , .tm_mon = 1 , .tm_year = 1963 };
int timebirth = mktime( &birthday_tm );
int diff_sec = timenow - timebirth;
int days = diff_sec / ( 24 * 60 * 60);
printf("%d - %d = %d seconds, or %d days\n", timenow, timebirth, diff_sec, days);

The bug in this particular code is that mktime() can't deal with dates before the epoch, which is Jan 1 1970 in Unix based systems. Similar issues will exist on other systems depending on how they count time. The general algorithm should work, you just have to know your limits on specific systems.

Shannon Nelson
btw, I voted for this, but you probably should have used time_t instead of int. mktime() is a great function.
dicroce
Yeah, time_t would perhaps be better. Thus an example of the fun of C, you can do all sorts of fun tricks with type names. At least I compiled and ran the code before I posted :-).
Shannon Nelson
...except that there isn't a fixed number of seconds in a day. On the daylight savings boundary, days are an hour longer or shorter. And that's different all over the world.
Marcus Downing
+6  A: 

First, note the assumption that if, for example, you were born on February 1st then on the following March 1st, you are exactly 1 month old, even though your age in days is only 28 -- less than the length of an average month. Years also have variable length (due to leap years) which means your age on your birthday is usually not an exact integer number of years. If you want to express the exact amount of time you've been alive in years/months/days, see my other answer. But more likely you want to fudge it just right so that being born on February 1st means that every February 1st you are X years, 0 months, and 0 days old, and on the 1st of any month you are X years, Y months, and 0 days.

In that case, read on. (NB: the following only works for dates in the past.)

Given a date of birth, (y,m,d), the current date, (ynow,mnow,dnow), and a function tm() that gives unix/epoch time for a given date, the following will output a 3-element list giving age as {years, months, days}:

t0 = y*12 + m - 1;        # total months for birthdate.
t = ynow*12 + mnow - 1;   # total months for Now.
dm = t - t0;              # delta months.    
if(dnow >= d) return [floor(dm/12), mod(dm,12), dnow-d];
dm--; t--;
return [floor(dm/12), mod(dm,12), 
        (tm({ynow,mnow,dnow}) - tm({floor(t/12), mod(t,12)+1, d}))/60/60/24];

Following is an equivalent algorithm if you don't like all the floors and mods. But I think the above is better. For one thing it avoids calling tm() when it doesn't need to.

{yl, ml} = {ynow, mnow};
if(mnow < m || mnow == m && dnow < d) yl--;
if(dnow < d) ml--;
years = yl - y;
months = ml + 12*(ynow - yl) - m;
yl = ynow;
if(ml == 0) { ml = 12; yl--; }
days = (tm({ynow, mnow, dnow}) - tm({yl, ml, d}))/60/60/24;
return [years, months, days];
dreeves
Btw, I believe this does the right thing for people born on February 29. Namely, they are one day short of being an integer number of years on Feb 28 and one day more than an integer number of years on Mar 1, regardless of whether there was a Feb 29.
dreeves
A: 

I think you have to specify the requirements a bit better.

Let's say the birth date is 2008-02-01 and the day is 2009-01-31

What is the result?

  • 0 years, 11 months and 31 days?
  • 0 years, 10 months and 28+31=59 days?
  • 0 years, 10 months and 29+31=60 days?
John Nilsson
A: 

Here is this using the borrowing rule of mathematics.

        DateTime dateOfBirth = new DateTime(2000, 4, 18);
        DateTime currentDate = DateTime.Now;

        int ageInYears = 0;
        int ageInMonths = 0;
        int ageInDays = 0;

        ageInDays = currentDate.Day - dateOfBirth.Day;
        ageInMonths = currentDate.Month - dateOfBirth.Month;
        ageInYears = currentDate.Year - dateOfBirth.Year;

        if (ageInDays < 0)
        {
            ageInDays += DateTime.DaysInMonth(currentDate.Year, currentDate.Month);
            ageInMonths = ageInMonths--;

            if (ageInMonths < 0)
            {
                ageInMonths += 12;
                ageInYears--;
            }
        }
        if (ageInMonths < 0)
        {
            ageInMonths += 12;
            ageInYears--;
        }

        Console.WriteLine("{0}, {1}, {2}", ageInYears, ageInMonths, ageInDays);
Rajeshwaran S P