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

Introduction:

We have seen so many c#.net basic examples. In this tutorial i am explaining about how to calculate age using c#.net. This is one of the best C#.net Interview Questions and Answers.

Description:

    class Program
    {
        static void Main(string[] args)
        {
            DateTime birthday = new DateTime(1984, 3, 26);
            DateTime today = DateTime.Today;
            int totalAge = today.Year - birthday.Year;

            if (birthday > today.AddYears(-totalAge)) totalAge--;

            Console.WriteLine("Age:" + totalAge);
            Console.Read();
        }
    }

Output: 

age:31

If you have any queries or suggestions, please feel free to ask in comments section.
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar