Validate DateTime Format

Test a C# date and time as a valid DateTime object format through error handling.

  1. public static bool IsDate(object attemptedDate)
  2. {
  3.  bool Success;
  4.  try
  5.  {
  6.                 DateTime dtParse = DateTime.Parse(attemptedDate.ToString());
  7.                 Success = true// If this line is reached, no exception was thrown
  8.  }
  9.  catch (FormatException e)
  10.  {
  11.                 Success = false// If this line is reached, an exception was thrown
  12.  }
  13.  return Success;
  14. }

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top