Friday, August 31, 2007

IsDate equivalent in C#

We need not load the entire Microsoft.VisualBasic.Information.IsDate() for this function alone...

We can use the following the basepage

public static bool IsDate(object Expression)
{
string strDate = Expression.ToString();
try
{
DateTime dt = DateTime.Parse(strDate);
if (dt != DateTime.MinValue && dt != DateTime.MaxValue)
return true;
return false;
}
catch
{
return false;
}
}

1 comment:

Tony Wright said...

I believe you should by using TryParse instead of Parse. TryParse does not raise an exception.