none
I can't get the final code RRS feed

  • 問題

  •  string ID;
                Console.WriteLine("Enter Your Birth");
                ID = Console.ReadLine();



                if (ID.ToString().Length != 8)
                {
                    Console.Write("Invaild");
                }
                else
                {
                    string now = DateTime.Now.ToString("dd/MM/yyyy");

                    now=now.Replace("/", "");
                    now=now.Replace(":", "");
                  now=now.Replace(" ", "");
                    now=now.Replace("AM", "");
                    now=now.Replace("PM", "");
                    
                    Console.Write(now-ID);
    2019年8月4日 3:26

解答

  • Hi selvatori,

    Thank you for posting here.

    Based on your code, you want to calculate the age refer to the birthday.

    You could try the following code.

     static void Main(string[] args)
            {
                string ID;
                Console.WriteLine("Enter Your Birth");
                ID = Console.ReadLine();
    
                DateTime dt;
    
                if (!DateTime.TryParse(ID, out dt))
                {
                    Console.Write("Invaild");
                }
                else
                {
                    string a = CalculateYourAge(dt);
                    Console.WriteLine(a);
                }
    
                Console.ReadKey();
            }
            static string CalculateYourAge(DateTime Dob)
            {
                DateTime Now = DateTime.Now;
                int Years = new DateTime(DateTime.Now.Subtract(Dob).Ticks).Year - 1;
                DateTime PastYearDate = Dob.AddYears(Years);
                int Months = 0;
                for (int i = 1; i <= 12; i++)
                {
                    if (PastYearDate.AddMonths(i) == Now)
                    {
                        Months = i;
                        break;
                    }
                    else if (PastYearDate.AddMonths(i) >= Now)
                    {
                        Months = i - 1;
                        break;
                    }
                }
                int Days = Now.Subtract(PastYearDate.AddMonths(Months)).Days;
                int Hours = Now.Subtract(PastYearDate).Hours;
                return String.Format("Age: {0} Year(s) {1} Month(s) {2} Day(s)",
                Years, Months, Days);
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2019年8月5日 3:30
    版主

所有回覆

  • Your code is missing a final brace (curly brace). Add a "}" at the end. That is the final code you are seeking.


    Sam Hobbs
    SimpleSamples.Info

    2019年8月4日 4:06
  • using System;
    using static System.Console;
    namespace ConsoleApp6
    {
        class Program
        {
            static void Main(string[] args)
            {
                WriteLine("Enter your birth:(ddMMyyyy)");
                string input = ReadLine();

                if(input.Length!=8)
                {
                    Console.Write("Invaild");
                }
                else
                {
                    DateTime ID = DateTime.ParseExact(input, "ddMMyyyy", null);
                    TimeSpan timeSpan = DateTime.Now-ID;
                    DateTime Age = DateTime.MinValue+timeSpan;
                    WriteLine($"Years: {Age.Year-1}");
                    WriteLine($"Months: {Age.Month}");
                    WriteLine($"Days: {Age.Day}");
                    ReadLine();
                }
            }
        }
    }




    2019年8月4日 13:11
  • Hi selvatori,

    Thank you for posting here.

    Based on your code, you want to calculate the age refer to the birthday.

    You could try the following code.

     static void Main(string[] args)
            {
                string ID;
                Console.WriteLine("Enter Your Birth");
                ID = Console.ReadLine();
    
                DateTime dt;
    
                if (!DateTime.TryParse(ID, out dt))
                {
                    Console.Write("Invaild");
                }
                else
                {
                    string a = CalculateYourAge(dt);
                    Console.WriteLine(a);
                }
    
                Console.ReadKey();
            }
            static string CalculateYourAge(DateTime Dob)
            {
                DateTime Now = DateTime.Now;
                int Years = new DateTime(DateTime.Now.Subtract(Dob).Ticks).Year - 1;
                DateTime PastYearDate = Dob.AddYears(Years);
                int Months = 0;
                for (int i = 1; i <= 12; i++)
                {
                    if (PastYearDate.AddMonths(i) == Now)
                    {
                        Months = i;
                        break;
                    }
                    else if (PastYearDate.AddMonths(i) >= Now)
                    {
                        Months = i - 1;
                        break;
                    }
                }
                int Days = Now.Subtract(PastYearDate.AddMonths(Months)).Days;
                int Hours = Now.Subtract(PastYearDate).Hours;
                return String.Format("Age: {0} Year(s) {1} Month(s) {2} Day(s)",
                Years, Months, Days);
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2019年8月5日 3:30
    版主
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年8月21日 8:03
    版主