ExtensionMethod.NET Home of 876 C#, Visual Basic, F# and Javascript extension methods

IsDate

Wraps DateTime.TryParse() and all the other kinds of code you need to determine if a given string holds a value that can be converted into a DateTime object.

Source

public static bool IsDate(this string input)
{
    if (!string.IsNullOrEmpty(input))
    {
        DateTime dt;
        return (DateTime.TryParse(input, out dt));
    }
    else
    {
        return false;
    }
}

Example

string nonDate = "Foo";
string someDate = "Jan 1 2010";

bool isDate = nonDate.IsDate(); //false
isDate = someDate.IsDate(); //true

Author: Phil Campbell

Submitted on: 30 mrt 2010

Language: C#

Type: System.String

Views: 17019