Check whether variable value is DateTime or not.

Tripati.patro
Posted by Tripati.patro under C# category on | Points: 40 | Views : 9117
private void NumericCheckMethod()
{
var inputString = "DotNetFunda";
if (!IsDateTime(inputString))
{
MessageBox.Show("InValid DateTime.");
}

object inputObject = null;
if (!IsDateTime(inputObject))
{
MessageBox.Show("Input is null");
}

var fundaUserId = DateTime.Now;
if (IsDateTime(fundaUserId))
{
MessageBox.Show("Valid DateTime.");
}
}

/// <summary>
/// Check whether a provided input is valid datetime or not.
/// </summary>
/// <param name="input"></param>
/// <returns>True: if input is DateTime type.</returns>
private bool IsDateTime(object input)
{
var isNumeric = false;
DateTime actualValue;
if (input != null && DateTime.TryParse(input.ToString(), out actualValue))
{
isNumeric = true;
}

return isNumeric;
}

Comments or Responses

Login to post response