【问题标题】:Is there any way to set Date only through ReadLine() to a DateTime type variable? How?有没有办法仅通过 ReadLine() 将 Date 设置为 DateTime 类型变量?如何?
【发布时间】:2015-09-03 16:33:39
【问题描述】:

我知道我可以做到这一点并且它有效

string Dob; //string
Console.WriteLine("Enter date of Birth in format DD/MM/YYYY: ");
Dob = Console.ReadLine();

但我想要这样的东西!预定义的方法或简写方式

DateTime Dob; //DateTime
Console.WriteLine("Enter date of Birth in format DD/MM/YYYY: ");
//What i am expecting, but is not possible as its DateTime 
Dob = Console.ReadLine(); // expects a string

是否有特定的方法可以直接从键盘获取日期到 Dob 变量中。

在 DateTime 类中是否有预定义的方法? 实现这一目标的最佳方式或最短方式是什么?

【问题讨论】:

  • DateTime.Parse(Console.ReadLine())?
  • @cubrr 它对我不起作用,它会抛出 System.FormatException
  • 那么你给它的日期格式错误。
  • @cubrr 我上次检查 20/12/2015 确实符合 dd/MM/yyyy 格式
  • @Black0ut DateTime.Parse(String) 使用 DateTimeFormatInfo.CurrentInfo。查看DateTime.Now.ToString(DateTimeFormatInfo.CurrentInfo) 的格式。

标签: c# datetime console-application


【解决方案1】:
string line = Console.ReadLine();
DateTime dt;
while (!DateTime.TryParseExact(line, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out dt))
{
    Console.WriteLine("Invalid date, please retry");
    line = Console.ReadLine();
}

【讨论】:

  • 这可行,但在我所在的伊利诺伊州芝加哥地区,默认日期样式为 MM/DD/YYYY。也许这是默认设置,没有设置为 DateTimeStyles 属性。您的意思是输入 mm/dd/yyyy 而不是 dd/mm/yyyy。此外,我完全看到了我们的日期格式的不合逻辑性质,因为它应该从最大到最小或者相反,但是在调试这段代码时我有点困惑。
【解决方案2】:

您可以通过编写如下扩展函数来扩展 .net 中的字符串类。

public static class Extensions
{
    public static DateTime? Dob(this string strLine)
    {
        DateTime result;
        if(DateTime.TryParseExact(strLine, "dd/MM/yyyy",System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal,out result))
        {
            return result;
        }
        else 
        {
            return null;
        }
    }
}

然后您可以在任何字符串对象上使用它。请参阅下面的示例以了解我在说什么。

    class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Please enter your date of birth in DD/MM/YYYY:");

        // Option 1.
        // The Console.ReadLine() function returns a string.
        string strDob = Console.ReadLine();
        DateTime? dob1 = strDob.Dob();


        // Option 2.
        // You can combine the two steps together into one line for smoother reading.
        DateTime? dob = Console.ReadLine().Dob();            
    }
}

注意:DateTime 类型末尾的问号 (?) 将 DateTime“结构”转换为可为空的“对象”。如果将 DateTime“结构”设置为 null,它将在 DateTime?可以设置为空。考虑使用 DateTime 是一种好习惯吗? (可为空)而不是使用 DateTime“结构”

【讨论】:

    【解决方案3】:

    此方法使用指定的格式和特定​​于区域性的格式信息将日期和时间的指定字符串表示形式转换为其等效的 DateTime。字符串表示的格式必须与指定的格式完全匹配。

    DateTime myDate = DateTime.ParseExact("2009/05/08","yyyy/MM/dd",
    System.Globalization.CultureInfo.InvariantCulture)
    

    【讨论】:

    • 感谢您发布此问题的答案!在 Stack Overflow 上不鼓励仅使用代码的答案,因为没有上下文的代码转储无法解释解决方案的工作方式或原因,这使得原始发布者(或任何未来的读者)难以理解其背后的逻辑。请编辑您的问题并包含对您的代码的解释,以便其他人可以从您的回答中受益。谢谢!
    【解决方案4】:

    到目前为止,正如cubrr 评论的那样,这是我发现的最简单和最短的方法。

    DateTime Dob;
    Console.WriteLine("Enter date of Birth in format MM/DD/YYYY: ");
    //accepts date in MM/dd/yyyy format
    Dob = DateTime.Parse(Console.ReadLine());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 2016-04-09
      • 2021-02-14
      • 2021-01-24
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多