【发布时间】: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