【代码】C#通过生日获取年龄_精确到天

2020-08-06 10:27:12  阅读 1641 次 评论 0 条

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





微信扫码查看本文
本文地址:https://www.yangguangdream.com/?id=2085
版权声明:本文为原创文章,版权归 编辑君 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?