Skip to content

Instantly share code, notes, and snippets.

@CheetahChrome
Created December 1, 2011 14:08
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 CheetahChrome/1416989 to your computer and use it in GitHub Desktop.
Save CheetahChrome/1416989 to your computer and use it in GitHub Desktop.
Extension String - Date Validation
public static bool ValidDate( this string date )
{
bool valid = false;
DateTime targetDate;
if (valid = DateTime.TryParse( date, out targetDate ))
valid = (targetDate >= new DateTime( 1753, 1, 1 )) &&
(targetDate <= new DateTime( 9999, 12, 31 ));
return valid;
}
Console.WriteLine( "12/31/1752".ValidDate()); // False
Console.WriteLine( "1/1/1753".ValidDate() ); // True
Console.WriteLine( DateTime.Now.ToShortDateString().ValidDate()); // True
Console.WriteLine( "12/31/9999".ValidDate() ); // True
Console.WriteLine( "1/1/10000".ValidDate() ); // False
@CheetahChrome
Copy link
Author

C# Extension for validating date within a range from an unknown string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment