'how can i calculate the age using the input (String) of the month,day and year?

        string Month, Year, Day;
        Console.Write("\nInsert birth month: ");
        Month = Console.ReadLine();

        if (Month.Equals("null"))
        {
            Console.Write("Insert birth day ");
            Console.Write("\nInsert birth year \n");

            
            Console.WriteLine("BIRTH MONTH:");
            Console.WriteLine("BIRTH DAY:");
            Console.WriteLine("BIRTH Year:");
            Console.WriteLine("BIRTHDAY:");


            Console.ReadKey();
        }
        else
        {
            Console.Write("Insert birth day: ");
            Day = Console.ReadLine();
            Console.Write("Insert birth year: ");
            Year = Console.ReadLine();

            
            Console.WriteLine("BIRTH MONTH: {0}", Month);
            Console.WriteLine("BIRTH DAY: {0}", Day);
            Console.WriteLine("BIRTH YEAR: {0}", Year);
            Console.WriteLine("BIRTHDAY:{0}/{1}/{2}", Month, Day, Year);
           Console.WriteLine("Age: {0}", DateTime.Now.Year - Convert.ToInt32(Year));
           Console.ReadKey();

           the only  variable that i can calculate is the year but when it comes to the month 
           and day it does not show correctly.

            The program runs like this:
            Insert birth month: 04
            Insert birth day:14
            Insert birth year:2000
         
            BIRTH MONTH:04
            BIRTH DAY:14
            BIRTH Year:2000
            BIRTHDAY:04/14/2000
            AGE: 22

The program runs like that because it does not considered the day and month to execute.

    The program should run like this:

            Insert birth month: 04
            Insert birth day:14
            Insert birth year:2000
         
            BIRTH MONTH:04
            BIRTH DAY:14
            BIRTH Year:2000
            BIRTHDAY:04/14/2000
            AGE: 21

As you can see the age is 21 because the month and day which is April 14 is being considered. any there who can help me out?

I used integer first but the "null" word is not working that's why i used string instead to work this out.

The program should run like this if i insert null work:

Insert birth month:null Insert birth day Insert birth year

BIRTH MONTH: BIRTH DAY: BIRTH Year: BIRTHDAY: AGE:

here's my concern how can i calculate age considering the Three main variable which is Month,Day and Year. Thanks in advance



Solution 1:[1]

  • Calculating age accurately needs lot of condition to check, because of leap year considerations Refer more on here : How do I calculate someone's age based on a DateTime type birthday?

    using System;
    
    namespace ConsoleApp2
    {
    internal class Program
    {
          static void Main(string[] args)
            {
    
                string FirstName, LastName = " ";
                char MiddleInitial = ' ';
    
                Console.Write("Insert  first name: ");
                FirstName = Console.ReadLine();
    
                if (FirstName.Equals("null"))
                {
                    Console.Write("Insert middle initial ");
                    Console.Write("\nInsert last name \n");
    
                }
                else
                {
                    Console.Write("Insert middle initial: ");
                    MiddleInitial = Console.ReadLine()[0];
                    Console.Write("Insert  last name: ");
                    LastName = Console.ReadLine();
                }
                string Month = "", Year = "", Day = "";
                Console.Write("\nInsert birth month: ");
                Month = Console.ReadLine();
    
    
                if (Month.Equals("null"))
                {
                    Console.Write("Insert birth day ");
                    Console.Write("\nInsert birth year \n");
    
                    Console.WriteLine("\nYour record");
                    Console.WriteLine("FIRST NAME:{0}", FirstName == "null" ? "" : FirstName);
                    Console.WriteLine("MIDDLE INITIAL: {0}", MiddleInitial);
                    Console.WriteLine("LASTNAME: {0}", LastName);
                    Console.WriteLine("BIRTH MONTH:");
                    Console.WriteLine("BIRTH DAY:");
                    Console.WriteLine("BIRTH Year:");
                    Console.WriteLine("BIRTHDAY:");
    
    
                    Console.ReadKey();
                }
                else
                {
                    Console.Write("Insert birth day: ");
                    Day = Console.ReadLine();
                    Console.Write("Insert birth year: ");
                    Year = Console.ReadLine();
    
                    var DOB = string.Format($"{Day}/{Month }  / {Year}");
    
                    var currentdate = DateTime.Now;
                    var age = Age(DateTime.Parse(DOB), currentdate);
    
                    Console.WriteLine("\nYour record");
                    Console.WriteLine("FIRST NAME:{0}", FirstName == "null" ? "" : FirstName);
                    Console.WriteLine("MIDDLE INITIAL: {0}", MiddleInitial);
                    Console.WriteLine("LASTNAME: {0}", LastName);
                    Console.WriteLine("BIRTH MONTH: {0}", Month);
                    Console.WriteLine("BIRTH DAY: {0}", Day);
                    Console.WriteLine("BIRTH YEAR: {0}", DateTime.Now.Year - Convert.ToInt32(Year));
                    Console.WriteLine("BIRTHDAY:{0}/{1}/{2}", Month, Day, Year);
                    Console.WriteLine("Age: {0} ", age);
                }
            }
    
            public static int Age(DateTime birthDate, DateTime laterDate)
            {
                int age;
                age = laterDate.Year - birthDate.Year;
    
                if (age > 0)
                {
                    age -= Convert.ToInt32(laterDate.Date < birthDate.Date.AddYears(age));
                }
                else
                {
                    age = 0;
                }
    
                return age;
            }
    
    
        }
    
    
    }
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1