如何以年、月、日为单位计算一个人的年龄? [英] How can I calculate the age of a person in year, month, days?

查看:64
本文介绍了如何以年、月、日为单位计算一个人的年龄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据出生日期和当前日期(相对于当前日期的年、月和日)计算一个人的年龄.

例如:

<预><代码>>>>计算年龄(2008, 01, 01)1年0个月16天

任何指向执行此操作的算法的指针将不胜感激.

解决方案

首先,请注意假设,例如,如果您出生于 2 月 1 日,那么在接下来的 3 月 1 日,您刚好 1 个月大,即使你的天数只有 28 岁——还不到一个月的平均长度.年份也有可变长度(由于闰年),这意味着您生日的年龄通常不是精确的整数年数.如果你想表达你以年/月/日为单位活着的确切时间,请参阅我的另一个答案.但更有可能的是你想捏造它,所以出生在 2 月 1 日意味着每个 2 月 1 日你都是 X 年零 0 个月零 0 天,而在任何一个月的 1 日,你是 X 年零 Y 个月,和 0 天.

在这种情况下,请继续阅读.(注意:以下仅适用于过去的日期.)

给定出生日期、(y,m,d)、当前日期、(ynow,mnow,dnow) 和函数 tm() 给出给定日期的unix/epoch时间,下面将输出一个以 {年、月、日}表示年龄的 3 元素列表:

t0 = y*12 + m - 1;# 生日的总月数.t = ynow*12 + mnow - 1;# 现在的总月数.dm = t - t0;# delta 月份.if(dnow >= d) 返回 [floor(dm/12), mod(dm,12), dnow-d];dm--;t--;返回 [地板 (dm/12), mod(dm,12),(tm({ynow,mnow,dnow}) - tm({floor(t/12), mod(t,12)+1, d}))/60/60/24];

如果您不喜欢所有楼层和模组,以下是等效的算法.但我认为上面的更好.一方面,它避免在不需要时调用 tm().

{yl, ml} = {ynow, mnow};if(mnow < m || mnow == m && dnow < d) yl--;if(dnow < d) ml--;年 = y - y;月 = ml + 12*(ynow - yl) - m;y = ynow;如果(毫升 == 0){ 毫升 = 12;y--;}天 = (tm({ynow, mnow, dnow}) - tm({yl, ml, d}))/60/60/24;返回 [年、月、日];

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.

解决方案

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

这篇关于如何以年、月、日为单位计算一个人的年龄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆