Skip to main content

Code snippet for checking whether string is datetime in C#

- Advertisement -

/// <summary>
/// Method checks if passed string is datetime
/// </summary>
/// <param name="text">string text for checking</param>
/// <returns>bool - if text is datetime return true, else return false</returns>
public bool IsDateTime(string text)
{
	DateTime dateTime;
	bool isDateTime = false;

	// Check for empty string.
	if (string.IsNullOrEmpty(text))
	{
		return false;
	}

	isDateTime = DateTime.TryParse(text, out dateTime);

	return isDateTime;
}

- Advertisement -
- Advertisement -