给定代表某人生日的DateTime,我如何计算他们的年龄(以年为单位)?
当前回答
人们可以这样计算“年龄”(即“西方人”的方式):
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 birthDate = new DateTime(2000,3,1);
int age = (int)Math.Floor((DateTime.Now - birthDate).TotalDays / 365.25D);
另一个功能,不是我做的,而是在网上找到的,并做了一些改进:
public static int GetAge(DateTime birthDate)
{
DateTime n = DateTime.Now; // To avoid a race condition around midnight
int age = n.Year - birthDate.Year;
if (n.Month < birthDate.Month || (n.Month == birthDate.Month && n.Day < birthDate.Day))
age--;
return age;
}
我只想到了两件事:来自不使用公历的国家的人呢?DateTime。我认为现在是服务器特定的文化。我对实际使用亚洲日历一无所知,我不知道是否有一种简单的方法来转换日历之间的日期,但以防万一,你想知道4660年的中国人:-)
通过较少的转换和UtcNow,这段代码可以照顾闰年2月29日出生的人:
public int GetAge(DateTime DateOfBirth)
{
var Now = DateTime.UtcNow;
return Now.Year - DateOfBirth.Year -
(
(
Now.Month > DateOfBirth.Month ||
(Now.Month == DateOfBirth.Month && Now.Day >= DateOfBirth.Day)
) ? 0 : 1
);
}
看看这个:
TimeSpan ts = DateTime.Now.Subtract(Birthdate);
age = (byte)(ts.TotalDays / 365.25);
这是一个非常适合我的功能。没有计算,非常简单。
public static string ToAge(this DateTime dob, DateTime? toDate = null)
{
if (!toDate.HasValue)
toDate = DateTime.Now;
var now = toDate.Value;
if (now.CompareTo(dob) < 0)
return "Future date";
int years = now.Year - dob.Year;
int months = now.Month - dob.Month;
int days = now.Day - dob.Day;
if (days < 0)
{
months--;
days = DateTime.DaysInMonth(dob.Year, dob.Month) - dob.Day + now.Day;
}
if (months < 0)
{
years--;
months = 12 + months;
}
return string.Format("{0} year(s), {1} month(s), {2} days(s)",
years,
months,
days);
}
这里是一个单元测试:
[Test]
public void ToAgeTests()
{
var date = new DateTime(2000, 1, 1);
Assert.AreEqual("0 year(s), 0 month(s), 1 days(s)", new DateTime(1999, 12, 31).ToAge(date));
Assert.AreEqual("0 year(s), 0 month(s), 0 days(s)", new DateTime(2000, 1, 1).ToAge(date));
Assert.AreEqual("1 year(s), 0 month(s), 0 days(s)", new DateTime(1999, 1, 1).ToAge(date));
Assert.AreEqual("0 year(s), 11 month(s), 0 days(s)", new DateTime(1999, 2, 1).ToAge(date));
Assert.AreEqual("0 year(s), 10 month(s), 25 days(s)", new DateTime(1999, 2, 4).ToAge(date));
Assert.AreEqual("0 year(s), 10 month(s), 1 days(s)", new DateTime(1999, 2, 28).ToAge(date));
date = new DateTime(2000, 2, 15);
Assert.AreEqual("0 year(s), 0 month(s), 28 days(s)", new DateTime(2000, 1, 18).ToAge(date));
}
推荐文章
- HTTP POST返回错误:417“期望失败。”
- 如何在。net中创建和使用资源
- 为什么Path。以Path.DirectorySeparatorChar开头的文件名合并不正确?
- 如何在c#中获得正确的时间戳
- Linq选择列表中存在的对象(A,B,C)
- c# .NET中的App.config是什么?如何使用它?
- c#:如何获得一个字符串的第一个字符?
- String类中的什么方法只返回前N个字符?
- 如何在PowerShell格式化日期时间
- 我如何提高ASP。NET MVC应用程序性能?
- 更好的方法将对象转换为int类型
- 我可以将c#字符串值转换为转义字符串文字吗?
- 在c#中转换char到int
- c#中朋友的对等物是什么?
- 关键字使用virtual+override vs. new