通过输入更改月、年或日

Change month, year, or day by input

我正在创建一个应用程序来计算两个日期之间的天数。当您输入 12 或 december 时,我一直在想如何更改 Month 的值。我无法将它分配给 date1,因为它是只读的。

输入格式为13/12/202113/December/2021(欧盟)

目前所做的是用“/”或“”拆分输入,这样我就可以将数字分配给 days/month/years。

我当前的代码:

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ', '/');
            string[] input2 = Console.ReadLine().Split(' ', '/');

            var date1 = new DateTime(2000, 3, 1, 7, 0, 0);

            for (int i = 0; i < input.Length; i++)
            {
                input[i] = input[i].ToLower();
            }

            for (int j = 0; j < input2.Length; j++)
            {
                input2[j] = input2[j].ToLower();
            }

            
            if (input[2] == "december") { date1.Month = 12; }

            foreach (var item in input)
            {
                Console.Write(item + " ");
            }
        }
    }
}

您不能修改 DataTime(或任何其他 Value Type)。相反,您必须创建一个新的:

date1 = new DateTime(date1.Year, 12, date1.Day);

让我们提取方法进入DateTime:

   using System.Globalization;

   ...

   public static DateTime ReadDate(string title) {
     // Keep asking until correct date has been provided
     while (true) {
       if (!string.IsNullOrEmpty(title)) 
         Console.WriteLine(title);

       // We support 13/12/2021, 13/December/2021 and 13/Dec/2021
       if (DateTime.TryParseExact(Console.ReadLine(), 
             new string[] { "d/M/yyyy", "d/MMMM/yyyy", "d/MMM/yyyy"}, 
             null, // or CultureInfo.GetCultureInfo("Nl-nl") 
             DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeLocal, 
             out var result)) 
         return result;

       Console.WriteLine("Syntax error. Please, try again."); 
     }  
   }

那你就简单点吧

   static void Main(string[] args) {
     DateTime date1 = ReadDate("Enter the first date."); 
     DateTime date2 = ReadDate("Enter the second date."); 

     TimeSpan difference = date2 - date1;

     Console.WriteLine($"Difference between {date1} and {date2} is {difference.TotalDays:f0} days"); 
   }

这是我的解决方案。可能效率不高,但我还是个初学者

    using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string[] input = Console.ReadLine().Split(' ', '/');
                string[] input2 = Console.ReadLine().Split(' ', '/');

                var date1 = new DateTime();
                var date2 = new DateTime();

                for (int i = 0; i < input.Length; i++)
                {
                    input[i] = input[i].ToLower();
                }

                for (int j = 0; j < input2.Length; j++)
                {
                    input2[j] = input2[j].ToLower();
                }

                date1 = new DateTime(Convert.ToInt32(input[2]), 1, Convert.ToInt32(input[0]));

                if (input[1] == "1") { date1 = new DateTime(Convert.ToInt32(input[2]), 1, Convert.ToInt32(input[0])); }
                if (input[1] == "2") { date1 = new DateTime(Convert.ToInt32(input[2]), 2, Convert.ToInt32(input[0])); }
                if (input[1] == "3") { date1 = new DateTime(Convert.ToInt32(input[2]), 3, Convert.ToInt32(input[0])); }
                if (input[1] == "4") { date1 = new DateTime(Convert.ToInt32(input[2]), 4, Convert.ToInt32(input[0])); }
                if (input[1] == "5") { date1 = new DateTime(Convert.ToInt32(input[2]), 5, Convert.ToInt32(input[0])); }
                if (input[1] == "6") { date1 = new DateTime(Convert.ToInt32(input[2]), 6, Convert.ToInt32(input[0])); }
                if (input[1] == "7") { date1 = new DateTime(Convert.ToInt32(input[2]), 7, Convert.ToInt32(input[0])); }
                if (input[1] == "8") { date1 = new DateTime(Convert.ToInt32(input[2]), 8, Convert.ToInt32(input[0])); }
                if (input[1] == "9") { date1 = new DateTime(Convert.ToInt32(input[2]), 9, Convert.ToInt32(input[0])); }
                if (input[1] == "10") { date1 = new DateTime(Convert.ToInt32(input[2]), 10, Convert.ToInt32(input[0])); }
                if (input[1] == "11") { date1 = new DateTime(Convert.ToInt32(input[2]), 11, Convert.ToInt32(input[0])); }
                if (input[1] == "12") { date1 = new DateTime(Convert.ToInt32(input[2]), 12, Convert.ToInt32(input[0])); }

                if (input[1] == "januari") { date1 = new DateTime(date1.Year, 1, date1.Day); }
                if (input[1] == "februari") { date1 = new DateTime(date1.Year, 2, date1.Day); }
                if (input[1] == "maart") { date1 = new DateTime(date1.Year, 3, date1.Day); }
                if (input[1] == "april") { date1 = new DateTime(date1.Year, 4, date1.Day); }
                if (input[1] == "mei") { date1 = new DateTime(date1.Year, 5, date1.Day); }
                if (input[1] == "juni") { date1 = new DateTime(date1.Year, 6, date1.Day); }
                if (input[1] == "juli") { date1 = new DateTime(date1.Year, 7, date1.Day); }
                if (input[1] == "augustus") { date1 = new DateTime(date1.Year, 8, date1.Day); }
                if (input[1] == "september") { date1 = new DateTime(date1.Year, 9, date1.Day); }
                if (input[1] == "oktober") { date1 = new DateTime(date1.Year, 10, date1.Day); }
                if (input[1] == "november") { date1 = new DateTime(date1.Year, 11, date1.Day); }
                if (input[1] == "december") { date1 = new DateTime(date1.Year, 12, date1.Day); }

                //
                date2 = new DateTime(Convert.ToInt32(input2[2]), 1, Convert.ToInt32(input2[0]));

                if (input2[1] == "1") { date2 = new DateTime(Convert.ToInt32(input2[2]), 1, Convert.ToInt32(input2[0])); }
                if (input2[1] == "2") { date2 = new DateTime(Convert.ToInt32(input2[2]), 2, Convert.ToInt32(input2[0])); }
                if (input2[1] == "3") { date2 = new DateTime(Convert.ToInt32(input2[2]), 3, Convert.ToInt32(input2[0])); }
                if (input2[1] == "4") { date2 = new DateTime(Convert.ToInt32(input2[2]), 4, Convert.ToInt32(input2[0])); }
                if (input2[1] == "5") { date2 = new DateTime(Convert.ToInt32(input2[2]), 5, Convert.ToInt32(input2[0])); }
                if (input2[1] == "6") { date2 = new DateTime(Convert.ToInt32(input2[2]), 6, Convert.ToInt32(input2[0])); }
                if (input2[1] == "7") { date2 = new DateTime(Convert.ToInt32(input2[2]), 7, Convert.ToInt32(input2[0])); }
                if (input2[1] == "8") { date2 = new DateTime(Convert.ToInt32(input2[2]), 8, Convert.ToInt32(input2[0])); }
                if (input2[1] == "9") { date2 = new DateTime(Convert.ToInt32(input2[2]), 9, Convert.ToInt32(input2[0])); }
                if (input2[1] == "10") { date2 = new DateTime(Convert.ToInt32(input2[2]), 10, Convert.ToInt32(input2[0])); }
                if (input2[1] == "11") { date2 = new DateTime(Convert.ToInt32(input2[2]), 11, Convert.ToInt32(input2[0])); }
                if (input2[1] == "12") { date2 = new DateTime(Convert.ToInt32(input2[2]), 12, Convert.ToInt32(input2[0])); }

                if (input2[1] == "januari") { date2 = new DateTime(date2.Year, 1, date2.Day); }
                if (input2[1] == "februari") { date2 = new DateTime(date2.Year, 2, date2.Day); }
                if (input2[1] == "maart") { date2 = new DateTime(date2.Year, 3, date2.Day); }
                if (input2[1] == "april") { date2 = new DateTime(date2.Year, 4, date2.Day); }
                if (input2[1] == "mei") { date2 = new DateTime(date2.Year, 5, date2.Day); }
                if (input2[1] == "juni") { date2 = new DateTime(date2.Year, 6, date2.Day); }
                if (input2[1] == "juli") { date2 = new DateTime(date2.Year, 7, date2.Day); }
                if (input2[1] == "augustus") { date2 = new DateTime(date2.Year, 8, date2.Day); }
                if (input2[1] == "september") { date2 = new DateTime(date2.Year, 9, date2.Day); }
                if (input2[1] == "oktober") { date2 = new DateTime(date2.Year, 10, date2.Day); }
                if (input2[1] == "november") { date2 = new DateTime(date2.Year, 11, date2.Day); }
                if (input2[1] == "december") { date2 = new DateTime(date2.Year, 12, date2.Day); }



                double output = (date2 - date1).TotalDays;

                Console.WriteLine(output + "days");
            }
            catch(Exception e)
            {
                Console.WriteLine("wrong input!");
            }
        }
    }
}