给定代表某人生日的DateTime,我如何计算他们的年龄(以年为单位)?
当前回答
以下是计算某人年龄的最简单方法。计算某人的年龄非常简单,下面是计算方法!为了使代码正常工作,您需要一个名为BirthDate的DateTime对象,该对象包含生日。
C#
// get the difference in years
int years = DateTime.Now.Year - BirthDate.Year;
// subtract another year if we're before the
// birth day in the current year
if (DateTime.Now.Month < BirthDate.Month ||
(DateTime.Now.Month == BirthDate.Month &&
DateTime.Now.Day < BirthDate.Day))
years--;
VB.NET
' get the difference in years
Dim years As Integer = DateTime.Now.Year - BirthDate.Year
' subtract another year if we're before the
' birth day in the current year
If DateTime.Now.Month < BirthDate.Month Or (DateTime.Now.Month = BirthDate.Month And DateTime.Now.Day < BirthDate.Day) Then
years = years - 1
End If
其他回答
SQL版本:
declare @dd smalldatetime = '1980-04-01'
declare @age int = YEAR(GETDATE())-YEAR(@dd)
if (@dd> DATEADD(YYYY, -@age, GETDATE())) set @age = @age -1
print @age
==常见说法(从几个月到几岁)===
如果您只是为了通用,以下是代码作为您的信息:
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.Now - myDate).TotalHours / 8766.0
当前日期-myDate=TimeSpan,获取总小时数并除以每年的总小时数,得到确切的年龄/月/日。。。
只是因为我认为最重要的答案不是那么明确:
public static int GetAgeByLoop(DateTime birthday)
{
var age = -1;
for (var date = birthday; date < DateTime.Today; date = date.AddYears(1))
{
age++;
}
return age;
}
人们可以这样计算“年龄”(即“西方人”的方式):
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;
}
分数是过去的时间(从上一个生日到下一个生日)与总时间的比率。
无论是向前还是向后,这两种方法都以相同的方式工作。
推荐文章
- 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