Created
April 10, 2012 19:58
-
-
Save darylf/2354060 to your computer and use it in GitHub Desktop.
Validate date format in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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