C# 根据出生日期(年月日)计算年龄的代码

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;
    }

 

posted @ 2019-04-13 15:48  Aaronguo  阅读(8813)  评论(0编辑  收藏  举报