给定代表某人生日的DateTime,我如何计算他们的年龄(以年为单位)?


当前回答

我的建议

int age = (int) ((DateTime.Now - bday).TotalDays/365.242199);

这一年似乎在正确的日期发生了变化。(我在107岁之前进行了现场测试。)

其他回答

要使用最近的年龄计算年龄:

var ts = DateTime.Now - new DateTime(1988, 3, 19);
var age = Math.Round(ts.Days / 365.0);

人们可以这样计算“年龄”(即“西方人”的方式):

public static int AgeInYears(this System.DateTime source, System.DateTime target)
  => target.Year - source.Year is int age && age > 0 && source.AddYears(age) > target ? age - 1 : age < 0 && source.AddYears(age) < target ? age + 1 : age;

如果时间方向为“负”,则年龄也将为负。

可以添加一个分数,代表从目标到下一个生日的累计年龄:

public static double AgeInTotalYears(this System.DateTime source, System.DateTime target)
{
  var sign = (source <= target ? 1 : -1);

  var ageInYears = AgeInYears(source, target); // The method above.

  var last = source.AddYears(ageInYears);
  var next = source.AddYears(ageInYears + sign);

  var fractionalAge = (double)(target - last).Ticks / (double)(next - last).Ticks * sign;

  return ageInYears + fractionalAge;
}

分数是过去的时间(从上一个生日到下一个生日)与总时间的比率。

无论是向前还是向后,这两种方法都以相同的方式工作。

==常见说法(从几个月到几岁)===

如果您只是为了通用,以下是代码作为您的信息:

DateTime today = DateTime.Today;
DateTime bday = DateTime.Parse("2016-2-14");
int age = today.Year - bday.Year;
var unit = "";

if (bday > today.AddYears(-age))
{
    age--;
}
if (age == 0)   // Under one year old
{
    age = today.Month - bday.Month;

    age = age <= 0 ? (12 + age) : age;  // The next year before birthday

    age = today.Day - bday.Day >= 0 ? age : --age;  // Before the birthday.day

    unit = "month";
}
else {
    unit = "year";
}

if (age > 1)
{
    unit = unit + "s";
}

测试结果如下:

The birthday: 2016-2-14

2016-2-15 =>  age=0, unit=month;
2016-5-13 =>  age=2, unit=months;
2016-5-14 =>  age=3, unit=months; 
2016-6-13 =>  age=3, unit=months; 
2016-6-15 =>  age=4, unit=months; 
2017-1-13 =>  age=10, unit=months; 
2017-1-14 =>  age=11, unit=months; 
2017-2-13 =>  age=11, unit=months; 
2017-2-14 =>  age=1, unit=year; 
2017-2-15 =>  age=1, unit=year; 
2017-3-13 =>  age=1, unit=year;
2018-1-13 =>  age=1, unit=year; 
2018-1-14 =>  age=1, unit=year; 
2018-2-13 =>  age=1, unit=year; 
2018-2-14 =>  age=2, unit=years; 

我在这个问题上使用了以下内容。我知道它不太优雅,但它很管用。

DateTime zeroTime = new DateTime(1, 1, 1);
var date1 = new DateTime(1983, 03, 04);
var date2 = DateTime.Now;
var dif = date2 - date1;
int years = (zeroTime + dif).Year - 1;
Log.DebugFormat("Years -->{0}", years);

为什么不能简化为检查出生日期?

第一行(var year=end.year-start.year-1;):假设出生日期尚未发生在结束年份。然后检查月份和日期,看看是否发生了;再增加一年。

对闰年情景没有特殊处理。如果不是闰年,你不能创建一个日期(2月29日)作为结束日期,所以如果结束日期是3月1日,而不是28日,生日庆祝活动将被计算在内。下面的函数将将此场景作为普通日期进行描述。

    static int Get_Age(DateTime start, DateTime end)
    {
        var year = end.Year - start.Year - 1;
        if (end.Month < start.Month)
            return year;
        else if (end.Month == start.Month)
        {
            if (end.Day >= start.Day)
                return ++year;
            return year;
        }
        else
            return ++year;
    }

    static void Test_Get_Age()
    {
        var start = new DateTime(2008, 4, 10); // b-date, leap year BTY
        var end = new DateTime(2023, 2, 1); // end date is before the b-date
        var result1 = Get_Age(start, end);
        var success1 = result1 == 14; // true

        end = new DateTime(2023, 4, 10); // end date is on the b-date
        var result2 = Get_Age(start, end);
        var success2 = result2 == 15; // true

        end = new DateTime(2023, 6, 22); // end date is after the b-date
        var result3 = Get_Age(start, end);
        var success3 = result3 == 15; // true

        start = new DateTime(2008, 2, 29); // b-date is on feb 29
        end = new DateTime(2023, 2, 28); // end date is before the b-date
        var result4 = Get_Age(start, end);
        var success4 = result4 == 14; // true

        end = new DateTime(2020, 2, 29); // end date is on the b-date, on another leap year
        var result5 = Get_Age(start, end);
        var success5 = result5 == 12; // true
    }