【问题标题】:How to enter datetime variables from keyboard in C#如何在 C# 中从键盘输入日期时间变量
【发布时间】:2020-01-25 19:13:50
【问题描述】:

我需要根据日期/月份/年份格式从我的键盘输入数据。

我尝试这样的代码:

   DateTime dt = new DateTime();
   dt = DateTime.Parse(Console.ReadLine());

但是当输入日期时,我还是先输入年份。那么我该如何解决这个问题。谢谢!

【问题讨论】:

  • 要么解析/上的输入拆分以分隔元素,要么使用3个提示。
  • 您输入的具体内容。
  • 可能是这样的DateTime dt = new DateTime(); dt = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy", CultureInfo.InvariantCulture); Console.WriteLine(dt.ToString("dd/MM/yyyy"));
  • @ChetanRanpariya 程序错误:dt = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy", CultureInfo.InvariantCulture);

标签: c# date datetime input keyboard


【解决方案1】:

不要忘记世界各地的日期格式不同。英国使用日/月/年,美国使用月/日/年,中国使用年/月/日。除此之外,分隔符也可以不同(有些使用斜线,有些文化使用破折号等)。

DateTime.Parse 将使用从操作系统启动时检测到的区域性。如果你想覆盖它,那么你应该使用DateTime.ParseExact(你指定确切的模式),或者传递你想要使用的文化DateTime.Parse

【讨论】:

    【解决方案2】:

    你能告诉我你在 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")}");
    

    【讨论】:

    • 看看 DateTime 构造函数doc。构造函数以该顺序接收int year, int month, int day,这与 OP 的目标格式不匹配。看看我的答案以获得简单的解决方案
    • 这里我们自己没有使用构造函数。我在 RoslynPad 中尝试过这段代码,它解析 12/14/2016 和 2016/12/14 完全相同。
    • 哦,抱歉,我应该在评论之前搜索一下 tryparse 的文档。很好的解决方案。
    【解决方案3】:

    首先,您必须导入

    using System.Globalization;
    

     在您的代码标题中,在您输入此代码后,它就可以工作

    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
    

    【讨论】:

      【解决方案4】:

      您可以查看我的解决方案here 的dotnetfiddle。

      如果您查看 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;

      【讨论】:

      • 程序错误:dt = DateTime.ParseExact(enteredDateString, targetDateFormat,CultureInfo.InvariantCulture);
      • using System.Globalization; 添加到代码顶部
      • 您期望的输入日期格式是日/月/年,对吧?此代码有效,您可以在点击运行并以上述格式输入日期时看到here。我建议从我发布的链接中复制代码并用它替换您的代码,看看会发生什么。您还可以编辑您的帖子并发布您的所有代码,以便我进一步提供帮助。
      • 哦,它有效!太感谢了!!!我意识到当我输入第 8 天和第 8 天时有很大的不同!!!这会导致程序错误
      猜你喜欢
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      相关资源
      最近更新 更多