calculate someone’s age in C#

In this post, we will learn about how to calculate someone’s age in C# with an example.

Description:

In my previous posts I have explained various topics like Why is Dictionary preferred over Hashtable, Introduction to BackboneJS, Bind an enumeration to combobox in WinForm, jQuery is undefined error: Common mistake every developer should avoid, 15 Powerful jQuery Tips and Tricks for Developers and Designer.


Now in this post, I will explain how to calculate someone’s age in C# with appropriate example.
Now create Console Application in Visual Studio and write below lines of code in it.

using System;
using System.Collections;
using System.Collections.Generic;
namespace DotNetK
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime _BirthDate = new DateTime(1991, 10, 29);
            DateTime _Now = new DateTime(2017, 12, 23);
            Console.WriteLine(string.Format("Age : {0}", GetAge(_BirthDate, _Now)));
            Console.ReadKey();
        }

        public static int GetAge(DateTime p_BirthDate, DateTime p_Now)
        {
            int age = p_Now.Year - p_BirthDate.Year;

            if (p_Now.Month < p_BirthDate.Month || (p_Now.Month == p_BirthDate.Month && p_Now.Day < p_BirthDate.Day))
                age--;

            return age;
        }
    }
}




Read :

Summary

You can also read about ASP.NET, C#.Net, JQUERY, AJAX, JavaScript
I hope you get an idea about how to calculate someone’s age in C#.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you liked this post, don’t forget to share this.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website