Skip to content

Instantly share code, notes, and snippets.

@darylf
Created April 10, 2012 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darylf/2354060 to your computer and use it in GitHub Desktop.
Save darylf/2354060 to your computer and use it in GitHub Desktop.
Validate date format in C#
/// <summary>
/// Validate date is a proper format
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static bool IsDate(string date)
{
bool valid = true;
//check in 99/99/9999 format
valid = Regex.IsMatch(date, "^[0-9]{2}/[0-9]{2}/[0-9]{4}$");
try
{
if (valid)
{
DateTime oDate = new DateTime();
oDate = Convert.ToDateTime(date);
}
}
catch
{
valid = false;
}
return valid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment