Monday, March 26, 2007

String Date Validator in C#

Simple string date validator.

I am a big fan of maintaining a library of simple and clean helper methods. Here is a simple and clean way to verify if a string formatted date is a valid date. This allows you to encapsulate the exception handling making it easy to use and very readable - another important coding practice.

private static bool IsDate(string sDate)
{
DateTime dt;
bool isDate = true;

try
{
dt = DateTime.Parse(sDate);
}
catch
{
isDate = false;
}

return isDate;
}

No comments: