How do I calculate someone's age in C#? *

Question

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

Answer

For some reason Jeff's code didn't seem simple enough. To me this seems simpler and easier to understand:

DateTime today = DateTime.Today;
int age = today.Year - bday.Year;
if (bday > today.AddYears(-age)) age--;
< br > via < a class="StackLink" href=" http://stackoverflow.com/questions/9/" >How do I calculate someone's age in C#?< /a>
Share on Google Plus

About Cinema Guy

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment