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

后端 未结 30 2172
名媛妹妹
名媛妹妹 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:33

    My suggestion

    int age = (int) ((DateTime.Now - bday).TotalDays/365.242199);
    

    That seems to have the year changing on the right date. (I spot tested up to age 107.)

提交回复
热议问题