Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

You can calculate your age using below C# program.

// C# program for age calculator
using System;
class CALAGE
{
    public static void CalculateAge(DateTime DayOfBirth)
    {
        var dt = DateTime.Now;
        var years = new DateTime(DateTime.Now.Subtract(DayOfBirth).Ticks).Year - 1;
        var pastYear = DayOfBirth.AddYears(years);
        var months = 0;
        for ( int i = 1; i <= 12; i++)
        {
            if (pastYear.AddMonths(i) == dt)
            {
                months = i;
            }
            else if (pastYear.AddMonths(i) >= dt)
            {
                months = i - 1;
                break;
            }
        }
        var days = dt.Subtract(pastYear.AddMonths(months)).Days;
        Console.WriteLine(string.Format("It's been {0} years, {1} months, and {2} days since your birthday", years,months, days));
    }
    // driver code to check the above function
    public static void Main()
    {
        DateTime dob = Convert.ToDateTime("1989/04/27");
        CalculateAge(dob);
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Interview Questions and Answers,You Need To Know

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is the difference between public static, publ... >>
<< What’s a multicast delegate in c#?...