C#通过生日获取年龄_精确到天
这个是通过时间对象去实现的
public static 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;
}
微信扫码查看本文
发表评论