如何在 C# 中从键盘输入日期时间变量
How to enter datetime variables from keyboard in C#
我需要根据日期/月份/年份格式从我的键盘输入数据。
我尝试这样的代码:
DateTime dt = new DateTime();
dt = DateTime.Parse(Console.ReadLine());
但是输入日期的时候还是先输入年份。那么我该如何解决这个问题。谢谢!
你能告诉我你在 Program.cs 中的 Main 方法吗?我想知道用逐字字符串文字标记 readline 是否有帮助。你也应该使用 TryParse 这样你就可以处理。
if (!DateTime.TryParse($@"{Console.ReadLine()}", out DateTime result))
{
throw new FormatException("The Date you entered is not a valid date");
}
Console.WriteLine($"{result.ToString("yyyy/MM/dd")}");
您可以查看我的解决方案的 dotnetfiddle here。
如果您查看 DateTime 构造函数 doc,您会发现该构造函数按 int year, int month, int day
的顺序接收,这与 OP 的目标格式不匹配。看一下我的答案以获得简单的解决方案。
您还可以找到下面的代码:
string targetDateFormat = "dd/MM/yyyy";
DateTime dt;
Console.WriteLine("Enter a date in the format day/month/year");
string enteredDateString = Console.ReadLine();
//This is assuming the string entered is of the correct format, I suggest doing some validation
dt = DateTime.ParseExact(enteredDateString, targetDateFormat,CultureInfo.InvariantCulture);
//This will print in month/day/year format
Console.WriteLine(dt);
//This will print in day/month/year format
Console.WriteLine(dt.ToString("dd/MM/yyyy"));
您需要在代码中添加以下 using 声明:
using System.Globalization;
别忘了世界各地的日期格式各不相同。英国使用 day/month/year,美国使用 month/day/year,中国使用 year/month/day。除此之外,分隔符也可以不同(有些使用斜杠,有些文化使用破折号等)。
DateTime.Parse
将使用 O/S 启动时检测到的文化。如果你想覆盖它,那么你应该使用DateTime.ParseExact
(你指定确切的模式),或者传递DateTime.Parse
你想使用的文化。
首先,您必须导入
using System.Globalization;
在您的代码 header 中使其工作,只需将此代码
DateTime dt;
dt = DateTime.Parse (Console.ReadLine ());
Console.WriteLine (dt.ToString ("d"
CultureInfo.CreateSpecificCulture ("NZ-in")));
Console.ReadLine ();
有关详细信息,请访问此网页
https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings
我需要根据日期/月份/年份格式从我的键盘输入数据。
我尝试这样的代码:
DateTime dt = new DateTime();
dt = DateTime.Parse(Console.ReadLine());
但是输入日期的时候还是先输入年份。那么我该如何解决这个问题。谢谢!
你能告诉我你在 Program.cs 中的 Main 方法吗?我想知道用逐字字符串文字标记 readline 是否有帮助。你也应该使用 TryParse 这样你就可以处理。
if (!DateTime.TryParse($@"{Console.ReadLine()}", out DateTime result))
{
throw new FormatException("The Date you entered is not a valid date");
}
Console.WriteLine($"{result.ToString("yyyy/MM/dd")}");
您可以查看我的解决方案的 dotnetfiddle here。
如果您查看 DateTime 构造函数 doc,您会发现该构造函数按 int year, int month, int day
的顺序接收,这与 OP 的目标格式不匹配。看一下我的答案以获得简单的解决方案。
您还可以找到下面的代码:
string targetDateFormat = "dd/MM/yyyy";
DateTime dt;
Console.WriteLine("Enter a date in the format day/month/year");
string enteredDateString = Console.ReadLine();
//This is assuming the string entered is of the correct format, I suggest doing some validation
dt = DateTime.ParseExact(enteredDateString, targetDateFormat,CultureInfo.InvariantCulture);
//This will print in month/day/year format
Console.WriteLine(dt);
//This will print in day/month/year format
Console.WriteLine(dt.ToString("dd/MM/yyyy"));
您需要在代码中添加以下 using 声明:
using System.Globalization;
别忘了世界各地的日期格式各不相同。英国使用 day/month/year,美国使用 month/day/year,中国使用 year/month/day。除此之外,分隔符也可以不同(有些使用斜杠,有些文化使用破折号等)。
DateTime.Parse
将使用 O/S 启动时检测到的文化。如果你想覆盖它,那么你应该使用DateTime.ParseExact
(你指定确切的模式),或者传递DateTime.Parse
你想使用的文化。
首先,您必须导入
using System.Globalization;
在您的代码 header 中使其工作,只需将此代码
DateTime dt;
dt = DateTime.Parse (Console.ReadLine ());
Console.WriteLine (dt.ToString ("d"
CultureInfo.CreateSpecificCulture ("NZ-in")));
Console.ReadLine ();
有关详细信息,请访问此网页
https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings