In C#, how do I calculate someone's age based on a DateTime type birthday?

后端 未结 30 2116
名媛妹妹
名媛妹妹 2020-11-21 05:14

Given a DateTime representing a person\'s birthday, how do I calculate their age in years?

30条回答
  •  广开言路
    2020-11-21 05:52

    The best way that I know of because of leap years and everything is:

    DateTime birthDate = new DateTime(2000,3,1);
    int age = (int)Math.Floor((DateTime.Now - birthDate).TotalDays / 365.25D);
    

提交回复
热议问题