我正在創建一個計算兩個日期之間的天數的應用程式。我被困在如何更改Month您輸入 12 或 december 時的值。我無法將其分配給它,date1因為它是只讀的。
輸入格式為13/12/2021or 13/December/2021(EU)
目前所做的是用“/”或“”分割輸入,這樣我就可以將數字分配給天/月/年。
我目前的代碼:
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 " ");
}
}
}
}
uj5u.com熱心網友回復:
您不能修改 a DataTime(或任何其他值型別)。相反,您必須創建一個新的:
date1 = new DateTime(date1.Year, 12, date1.Day);
uj5u.com熱心網友回復:
讓我們提取進入的方法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");
}
uj5u.com熱心網友回復:
這是我的解決方案。它可能效率低下,但我仍然是初學者。
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!");
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/357909.html
