Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Feb 26, 2009 05:20 AM by Rimbik
Apr 20, 2008 07:23 AM|LINK
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.
Apr 20, 2008 08:08 AM|LINK
i did not found any builtin method you can use this one
public static bool IsDate(Object obj)
string strDate = obj.ToString();
DateTime dt = DateTime.Parse(strDate);
if((dt.Month!=System.DateTime.Now.Month) || (dt.Day<1&&dt.Day>31) || dt.Year!=System.DateTime.Now.Year)
Apr 20, 2008 06:28 PM|LINK
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,
Apr 20, 2008 08:38 PM|LINK
I solved the problem with the following code:
bool CheckDate(String date)
DateTime dt =
Thanks very much for your help.
Apr 21, 2008 06:46 AM|LINK
Great, Thanks a lot to let me know the best solution of that problem. stay in touch please.
Nov 17, 2008 10:20 AM|LINK
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#.
Nov 17, 2008 12:03 PM|LINK
This may run a bit faster because of the absence of the exception handler:
protected bool CheckDate(String date)
if (DateTime.TryParse(date, out Temp) == true)
Nov 17, 2008 12:24 PM|LINK
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!
Feb 06, 2009 11:01 AM|LINK
Very optimized code, Thanks a lot.
Feb 20, 2009 10:50 AM|LINK
this function will validate "12:50" also as a date time...
Any solutions for this?