How would you calculate the age in C# using date of birth (considering leap years)

前端 未结 6 1858
傲寒
傲寒 2020-12-21 07:02

Here is a problem. I have seen many solutions, but no one seems to be fulfilling the criteria I want...

I want to display the age in this format

20 y         


        
6条回答
  •  暖寄归人
    2020-12-21 07:24

    static int AgeInYears(DateTime birthday, DateTime today)
    {
        return ((today.Year - birthday.Year) * 372 + (today.Month - birthday.Month) * 31 + (today.Day - birthday.Day)) / 372;
    }
    

提交回复
热议问题