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];