HOW DO I CALCULATE SOMEONE'S AGE

Jeet Verma

Total Post:110

Points:776
Posted by  Jeet Verma
C# 
C#
 953  View(s)
Ratings:
Rate this:
Hi Expert!

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

Thanks in advance. 
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How do I calculate someone's age

    Hi Jeet!


    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--;

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!