Test a C# date and time as a valid DateTime object format through error handling.
public static bool IsDate(object attemptedDate)
- {
bool Success;
-
try
{
DateTime dtParse = DateTime.Parse(attemptedDate.ToString());
Success = true; // If this line is reached, no exception was thrown
}
catch (FormatException e)
- {
Success = false; // If this line is reached, an exception was thrown
}
return Success;
- }