我想检查string
是否包含1/01/2000
格式的日期,例如10/01/2000
和dd/MM/yyyy
。
到目前为止,我已经尝试过了。
DateTime dDate = DateTime.Parse(inputString);
string.Format("{0:d/MM/yyyy}", dDate);
但是,如何检查该格式对
throw an exception
是否正确? 最佳答案
string inputString = "2000-02-02";
DateTime dDate;
if (DateTime.TryParse(inputString, out dDate))
{
String.Format("{0:d/MM/yyyy}", dDate);
}
else
{
Console.WriteLine("Invalid"); // <-- Control flow goes here
}