Ali
Ali

Reputation: 1841

Checking if valid date in C# not working properly

I am using the following function to determine whether input string is valid date or not.

public static bool IsDate(string date)
        {
            DateTime Temp;

            if (DateTime.TryParse(date, out Temp))
                return true;
            else
                return false;
        }

Problem is when I give input "1997-09" then it returns true. I want it to check complete date like "1997-08-12"

And no there is no fix date format. Input could also be "19-Feb-2012"

Upvotes: 1

Views: 3584

Answers (8)

Dummy01
Dummy01

Reputation: 1995

Your code will run just fine and check the given date string if it can be a valid date using all of the current culture's date formats including this 19-Feb-2012 or 1997-09 of yours or even 19 february.

This makes you flexible in date input.

But if flexibility is not what your are looking for then try to parse for one or more specific formats using TryParseExact.

Upvotes: 0

Antonio Bakula
Antonio Bakula

Reputation: 20693

You should establish list of a correct date formats and then check with DateTime.TryParseExact, something like this:

string format = "yyyy-MM-dd";
DateTime dateTime;
if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture,
    DateTimeStyles.None, out dateTime))

Upvotes: 2

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

You can return result of parsing directly:

public static bool IsDate(string value)
{
    DateTime date;
    return DateTime.TryParse(value, out date);
}

And it works with formats you have provided (at least when current culture "en-US").

Upvotes: 0

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28538

one easy condition you can add:

    public static bool IsDate(string date)
    {
        DateTime Temp;
        return(DateTime.TryParse(date, out Temp)&&date.Length>=10)
    }

Upvotes: 2

Jon Comtois
Jon Comtois

Reputation: 1854

DateTime.TryParse does have an overload that takes an IFormatProvider to allow specification of custom formats. You may need to define multiple IFormatPrividers to check the various strings you may expect as valid.

Also, rather than the if/else, you could also shorten your code a bit by

return DateTime.TryParse(date, out Temp);

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038770

And no there is no fix date format. Input could also be "19-Feb-2012"

There must be, otherwise it's nonesense. If you haven't defined how your system must behave you'd better stop coding and take a moment to define it.

You could use the TryParseExact method which allows you to specify one or more formats you would like to handle.

Upvotes: 3

M Afifi
M Afifi

Reputation: 4795

Use DateTime.TryParse, you can specify the format then, more here. http://msdn.microsoft.com/en-us/library/9h21f14e.aspx

Upvotes: 1

Tigran
Tigran

Reputation: 62246

To resolve this you should define a datetime-format in your applicaiton.

On any webpage you go, if you see a compilation form, you probabbly will will see some date field too, and near it something like:

DD-MM-YYYY, or MM/DD/YY, or somethign else .

Define for your application format, make it esplicit for the user and check on correctness according to your format.

Just an hypothetic example:

say user inserted a date and you store it into the string like DD-MM-YYYY, one of possible choice could be simply say :

if(dateTimeUserString.Split('-').Length < 3)
    //not valid string !

I repeat, this is just an example you should choose more appropriate way for your application.

Upvotes: 0

Related Questions