Last post Feb 26, 2009 06:20 AM by Rimbik
Apr 20, 2008 08:23 AM|sg2000|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 09:08 AM|ashrafur|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 07:28 PM|sg2000|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 09:38 PM|sg2000|LINK
I solved the problem with the following code:
bool CheckDate(String date)
DateTime dt =
Thanks very much for your help.
Apr 21, 2008 07:46 AM|ashrafur|LINK
Great, Thanks a lot to let me know the best solution of that problem. stay in touch please.
Nov 17, 2008 11:20 AM|edwardkieran|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 01:03 PM|SGWellens|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 01:24 PM|edwardkieran|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 12:01 PM|1Madhu|LINK
Very optimized code, Thanks a lot.
Feb 20, 2009 11:50 AM|login.arpit|LINK
this function will validate "12:50" also as a date time...
Any solutions for this?
Feb 20, 2009 04:52 PM|David Anton|LINK
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".
Feb 21, 2009 06:49 AM|Paul Linton|LINK
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)
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
Feb 26, 2009 06:20 AM|Rimbik|LINK
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;
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