我想检查string是否包含1/01/2000格式的日期,例如10/01/2000dd/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
}

09-07 09:32