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

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

    Keeping it simple (and possibly stupid:)).

    DateTime birth = new DateTime(1975, 09, 27, 01, 00, 00, 00);
    TimeSpan ts = DateTime.Now - birth;
    Console.WriteLine("You are approximately " + ts.TotalSeconds.ToString() + " seconds old.");
    

提交回复
热议问题