//error string date
string s = "2012-4-5-02";
DateTime dt;
if (DateTime.TryParse(s,out dt))
MessageBox.Show(dt.ToString("yyyy-MM-dd"));
else
MessageBox.Show("error date");
// output : 2012-04-05
// why it can output a good date format? not is display “error date”.
TryParseandParseare relatively lenient – more lenient than I personally tend to like.If you want a more rigorous parsing process, using
DateTime.TryParseExact.