CodeYun
CodeYun

Reputation: 749

Validate Date format using C# 4.0

In our existing website code I encountered the following piece of code:

private static bool IsDate(string inputText)
        {
            try
            {
                DateTime.Parse(inputText);
                return true;
            }
            catch
            {
                return false;
            }
        }

Can this piece of code be optimized by using .NET 4.0 framework feature?

Upvotes: 0

Views: 1759

Answers (2)

Michael Ross
Michael Ross

Reputation: 570

bool IsDate(string Input)
    {
        DateTime RV;
        return DateTime.TryParse(Input, out RV);
    }

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245499

No. But it could be optimized using other Framework features that have been around for a lot longer than 4.0:

private static bool IsDate(string inputText)
{
    DateTime d;
    return DateTime.TryParse(inputText, out d);
}

Upvotes: 5

Related Questions