Home
Manage Your Code
Snippet: Validate a DateTime string (without Try/Catch) (C#)
Title: Validate a DateTime string (without Try/Catch) Language: C#
Description: This function validates a string to see if it is a valid DateTime. This uses a DateTime.TryParse instead of a slow Try/Catch block. Views: 1693
Author: Berin Iwlew Date Added: 3/28/2008
Copy Code  
1/// <summary>

2/// This function validates a string to see if it is a valid DateTime.

3/// </summary>

4/// <param name="dateTimeToCheck">The string representing a DateTime</param>

5/// <returns>True if the string is a valid DateTime, False if not</returns>

6public static bool IsValidDateTime(string dateTimeToCheck)
7{
8    DateTime myDateTimeResult;
9    return DateTime.TryParse(dateTimeToCheck, out myDateTimeResult);
10}
Usage
bool isValid = IsValidDateTime("11/20/1980");