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

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

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

30条回答
  •  -上瘾入骨i
    2020-11-21 05:45

    TimeSpan diff = DateTime.Now - birthdayDateTime;
    string age = String.Format("{0:%y} years, {0:%M} months, {0:%d}, days old", diff);
    

    I'm not sure how exactly you'd like it returned to you, so I just made a readable string.

提交回复
热议问题