How can I calculate the age of a person in year, month, days?

前端 未结 11 1254
谎友^
谎友^ 2020-11-27 05:52

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:

<         


        
11条回答
  •  忘掉有多难
    2020-11-27 06:36

    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: How do I convert a date/time to epoch time (aka unix time -- seconds since 1970) in Perl?. 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)); }
    

提交回复
热议问题