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.
The method of calculating age involves the comparison of a person's date of birth with the date on which the age needs to be calculated. The date of birth is subtracted from the given date, which gives the age of the person. Age = Given date - Date of birth.
Q: How do you find out someone's age? Ans: To find out a person's age, all you need is that person's year of birth. After this, all you need to do now is subtract the birth year from the ongoing current year and you will have the age. This will help you to calculate the age from the date of birth.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With