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#?
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?
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#.
sg2000
Member
359 Points
154 Posts
Is there a function similar to IsDate for C# that checks valid dates?
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.
sg2000
date validate date formatting
ashrafur
Participant
930 Points
166 Posts
Re: Is there a function similar to IsDate for C# that checks valid dates?
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();
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;
}
}
http://www.ashrafur.com
sg2000
Member
359 Points
154 Posts
Re: Is there a function similar to IsDate for C# that checks valid dates?
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,
sg2000
sg2000
Member
359 Points
154 Posts
Re: Is there a function similar to IsDate for C# that checks valid dates?
Apr 20, 2008 08:38 PM|LINK
Ashrafur:
I solved the problem with the following code:
protected bool CheckDate(String date)
{
try
{
DateTime dt = DateTime.Parse(date);
return true;
}catch
{
return false;
}
}
Thanks very much for your help.
sg2000
ashrafur
Participant
930 Points
166 Posts
Re: Is there a function similar to IsDate for C# that checks valid dates?
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.
http://www.ashrafur.com
edwardkieran
Member
11 Points
9 Posts
Re: Is there a function similar to IsDate for C# that checks valid dates?
Nov 17, 2008 10:20 AM|LINK
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
SGWellens
All-Star
126029 Points
10309 Posts
Moderator
Re: Is there a function similar to IsDate for C# that checks valid dates?
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) { DateTime Temp; if (DateTime.TryParse(date, out Temp) == true) return true; else return false; }My blog
edwardkieran
Member
11 Points
9 Posts
Re: Is there a function similar to IsDate for C# that checks valid dates?
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!
E. Wellington
1Madhu
Member
317 Points
119 Posts
Re: Is there a function similar to IsDate for C# that checks valid dates?
Feb 06, 2009 11:01 AM|LINK
Very optimized code, Thanks a lot.
Regards,
Madhu,India
login.arpit
Participant
1263 Points
305 Posts
Re: Is there a function similar to IsDate for C# that checks valid dates?
Feb 20, 2009 10:50 AM|LINK
this function will validate "12:50" also as a date time...
Any solutions for this?
dotnetways.blogspot.com
magicalflex.blogspot.com