public int GetAgeByBirthdate(DateTime birthdate)
{
DateTime now = DateTime.Now;
int age = now.Year - birthdate.Year;
if (now.Month < birthdate.Month || (now.Month == birthdate.Month && now.Day < birthdate.Day))
{
age--;
}
return age < 0 ? 0 : age;
}C# 根据出生日期(年月日)计算年龄的代码
本文介绍了一个使用C#编写的简单方法,该方法通过传入一个出生日期来计算并返回一个人的当前年龄。考虑到月份和日期的影响,确保了计算结果的准确性。

被折叠的 条评论
为什么被折叠?



