给定代表某人生日的DateTime,我如何计算他们的年龄(以年为单位)?
当前回答
private int GetYearDiff(DateTime start, DateTime end)
{
int diff = end.Year - start.Year;
if (end.DayOfYear < start.DayOfYear) { diff -= 1; }
return diff;
}
[Fact]
public void GetYearDiff_WhenCalls_ShouldReturnCorrectYearDiff()
{
//arrange
var now = DateTime.Now;
//act
//assert
Assert.Equal(24, GetYearDiff(new DateTime(1992, 7, 9), now)); // passed
Assert.Equal(24, GetYearDiff(new DateTime(1992, now.Month, now.Day), now)); // passed
Assert.Equal(23, GetYearDiff(new DateTime(1992, 12, 9), now)); // passed
}
其他回答
这很简单,似乎符合我的需要。我为闰年的目的做了一个假设,即无论一个人选择什么时候庆祝生日,从技术上讲,他们都不会比自己大一岁,直到他们的上一个生日过去365天(即2月28日不会使他们大一岁)。
DateTime now = DateTime.Today;
DateTime birthday = new DateTime(1991, 02, 03);//3rd feb
int age = now.Year - birthday.Year;
if (now.Month < birthday.Month || (now.Month == birthday.Month && now.Day < birthday.Day))//not had bday this year yet
age--;
return age;
我强烈建议使用名为AgeCalculator的NuGet软件包,因为在计算年龄(闰年、时间成分等)时需要考虑很多事情,而且只有两行代码不能削减它。该库给您的时间不只是一年。它甚至在计算时考虑了时间成分,这样你就可以得到一个包含年、月、日和时间成分的准确年龄。更为先进的是,可以选择将2月29日作为闰年,而将2月28日作为非闰年。
我对DateTime一无所知,但我能做的就是:
using System;
public class Program
{
public static int getAge(int month, int day, int year) {
DateTime today = DateTime.Today;
int currentDay = today.Day;
int currentYear = today.Year;
int currentMonth = today.Month;
int age = 0;
if (currentMonth < month) {
age -= 1;
} else if (currentMonth == month) {
if (currentDay < day) {
age -= 1;
}
}
currentYear -= year;
age += currentYear;
return age;
}
public static void Main()
{
int ageInYears = getAge(8, 10, 2007);
Console.WriteLine(ageInYears);
}
}
有点困惑,但仔细看代码,这一切都是有意义的。
我对Mark Soen的答案做了一个小小的修改:我重写了第三行,以便可以更容易地解析表达式。
public int AgeInYears(DateTime bday)
{
DateTime now = DateTime.Today;
int age = now.Year - bday.Year;
if (bday.AddYears(age) > now)
age--;
return age;
}
为了清晰起见,我还将其转换为函数。
对此的简单答案是应用AddYears,如下所示,因为这是唯一一种将年份添加到闰年2月29日的本地方法,并获得普通年份2月28日的正确结果。
有些人认为3月1日是勒普林斯的生日,但.Net和任何官方规则都不支持这一点,也没有常见的逻辑解释为什么一些出生在2月的人应该在另一个月拥有75%的生日。
此外,Age方法可以作为DateTime的扩展添加。由此,您可以以最简单的方式获得年龄:
列表项目
int age=出生日期.age();
public static class DateTimeExtensions
{
/// <summary>
/// Calculates the age in years of the current System.DateTime object today.
/// </summary>
/// <param name="birthDate">The date of birth</param>
/// <returns>Age in years today. 0 is returned for a future date of birth.</returns>
public static int Age(this DateTime birthDate)
{
return Age(birthDate, DateTime.Today);
}
/// <summary>
/// Calculates the age in years of the current System.DateTime object on a later date.
/// </summary>
/// <param name="birthDate">The date of birth</param>
/// <param name="laterDate">The date on which to calculate the age.</param>
/// <returns>Age in years on a later day. 0 is returned as minimum.</returns>
public static int Age(this DateTime birthDate, DateTime laterDate)
{
int age;
age = laterDate.Year - birthDate.Year;
if (age > 0)
{
age -= Convert.ToInt32(laterDate.Date < birthDate.Date.AddYears(age));
}
else
{
age = 0;
}
return age;
}
}
现在,运行此测试:
class Program
{
static void Main(string[] args)
{
RunTest();
}
private static void RunTest()
{
DateTime birthDate = new DateTime(2000, 2, 28);
DateTime laterDate = new DateTime(2011, 2, 27);
string iso = "yyyy-MM-dd";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine("Birth date: " + birthDate.AddDays(i).ToString(iso) + " Later date: " + laterDate.AddDays(j).ToString(iso) + " Age: " + birthDate.AddDays(i).Age(laterDate.AddDays(j)).ToString());
}
}
Console.ReadKey();
}
}
关键日期示例如下:
出生日期:2000-02-29出生日期:2011-02-28年龄:11
输出:
{
Birth date: 2000-02-28 Later date: 2011-02-27 Age: 10
Birth date: 2000-02-28 Later date: 2011-02-28 Age: 11
Birth date: 2000-02-28 Later date: 2011-03-01 Age: 11
Birth date: 2000-02-29 Later date: 2011-02-27 Age: 10
Birth date: 2000-02-29 Later date: 2011-02-28 Age: 11
Birth date: 2000-02-29 Later date: 2011-03-01 Age: 11
Birth date: 2000-03-01 Later date: 2011-02-27 Age: 10
Birth date: 2000-03-01 Later date: 2011-02-28 Age: 10
Birth date: 2000-03-01 Later date: 2011-03-01 Age: 11
}
2012年2月28日晚些时候:
{
Birth date: 2000-02-28 Later date: 2012-02-28 Age: 12
Birth date: 2000-02-28 Later date: 2012-02-29 Age: 12
Birth date: 2000-02-28 Later date: 2012-03-01 Age: 12
Birth date: 2000-02-29 Later date: 2012-02-28 Age: 11
Birth date: 2000-02-29 Later date: 2012-02-29 Age: 12
Birth date: 2000-02-29 Later date: 2012-03-01 Age: 12
Birth date: 2000-03-01 Later date: 2012-02-28 Age: 11
Birth date: 2000-03-01 Later date: 2012-02-29 Age: 11
Birth date: 2000-03-01 Later date: 2012-03-01 Age: 12
}