Share via


Is there a function similar to IsDate for C# that checks valid dates?

Question

Sunday, April 20, 2008 3:23 AM

I would like to check for the validity of a date (such as a February date for leap year) in C#. I know in VB, you can use the IsDate function to do this. Is there a similar call in C#?

 Thanks for any suggestion.

sg2000

All replies (12)

Sunday, April 20, 2008 4:38 PM âś…Answered

Ashrafur:

I solved the problem with the following code:

protected bool CheckDate(String date)

{

   try

   {

        DateTime dt = DateTime.Parse(date);

        return true;

   }<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

   catch

  {

        return false;

  }

}

 

Thanks very much for your help.

sg2000


Sunday, April 20, 2008 4:08 AM

i did not found any builtin method you can use this one

public static bool IsDate(Object obj)
{
        string strDate = obj.ToString();
        try
        {
            DateTime dt = DateTime.Parse(strDate);
            if((dt.Month!=System.DateTime.Now.Month) || (dt.Day<1&&dt.Day>31) || dt.Year!=System.DateTime.Now.Year)
                return false;
            else
                return true;
       }
       catch
       {
            return false;
       }
}


Sunday, April 20, 2008 2:28 PM

Thank you very much, ashrafur. I tried the function you recommended and it works only for the current month and year. What I want is to validate the date for any month, day and year; furthermore, the day is to be validated against a particular month and year (not just within 1 - 31). For example, 2/29/2008 is OK, but 2/29/2007 is not (because 2007 is a leap year). Can you recommend how can I do this?

Thanks in advance,

sg2000

 

 


Monday, April 21, 2008 2:46 AM

Great, Thanks a lot to let me know the best solution of that problem. stay in touch please.


Monday, November 17, 2008 6:20 AM

 Hi sg2000,

I just faced with the problem and luckily saw your post, thank you very much for sharing your acquisition with people.

I do not understand why they do not give that VB  built in functionality to C# as well, I consider C# as more powerful than VB, and it is disappointing to see VB is more functional than C#.

Regards,

Edward

 


Monday, November 17, 2008 8:03 AM

This may run a bit faster because of the absence of the exception handler:

    protected bool CheckDate(String date)
    {
        DateTime Temp;

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

 


Monday, November 17, 2008 8:24 AM

 Well, Mr Wellens, I appreciate your elegant answer, thank you very much indeed, that was kind of you.

I am flattered by your points,by the way!

E. Wellington


Friday, February 6, 2009 7:01 AM

Very optimized code, Thanks a lot.


Friday, February 20, 2009 6:50 AM

 this function will validate "12:50" also as a date time...

Any solutions for this?


Friday, February 20, 2009 11:52 AM

Use 'TryParseExact'.

Note to S.G. Wellens:  You deleted my identical response because you said "TryParseExact" was already suggested.  This is incorrect - "TryParseExact" is not the same as "TryParse".

 


Saturday, February 21, 2009 1:49 AM

Unfortunately "12:50" is a valid DateTime.  You could check for the specified DateTime to have zero for the time components like this

bool IsDate(string input)
{
   DateTime temp;
   return DateTime.TryParse(input, CultureInfo.CurrentCulture, DateTimeStyles.NotCurrentDateDefault, out temp) &&
          temp.Hour == 0 &&
          temp.Minute == 0 &&
          temp.Second == 0 &&
          temp.Millisecond == 0 &&
          temp > DateTime.MinValue;
}

 IsDate("2/2/2002") == true

IsDate("12:50") == false

IsDate("2/2/2002 12:50") == false

IsDate("0:0") == false - equal to DateTime.MinValue and tested by (temp> DateTime.MinValue)

IsDate("0") == false

The long form of the TryParse method is required so that we can specify DateTimeStyles.NotCurrentDateDefault.  If you don't do this then "0:0" is parsed as being midnight of the current date and would thus be accepted as a valid date.

hope that helps


Thursday, February 26, 2009 1:20 AM

 isDate or something is availabel for VB or VB.NET only. however I am not a VB guy I would talk about c#.

C# code snippet:

DateTime d = DateTime.Today;
DateTime newDate;
bool result = DateTime.TryParse(d.ToShortDateString(), out newDate); // true means correct date
bool result1 = DateTime.TryParse("123", out newDate); // not a date

// Remember available only >= .NET Framework 2.0

 

Soumen