Best practice in checking if a string is a datetime before converting?
c#
Solution
Use the DateTime.TryParse method instead of using your own try/catch blocks.
string text = "10/16/2009";
DateTime result;
if (DateTime.TryParse(text, out result))
{
// success, result holds converted value
}
else
{
// failed
}
Problem
What's the best way to do it? This is how I'll usually do it: ``` DateTime newDate; try { newDate = DateTime.Parse(Textbox.Text); } catch { //isn't a datetime return; } //do stuff with the date ``` But something tells me that that is a bit wrong. Any ideas?