Hi,
How do i check if a string contains a valid date
Ex, i have a string ="jan 6 15:15", I want to set the year to 2006, but only if it's not already a valid date as is the case.
so, the resulting string should be "jan 6 2006 15:15". but if the string already has a valid date, ex "jan 7 2005", I don't want to touch it.
I try to use the datetime.parse inside a try/catch, but i couldn't get it to fail. It keeps setting the date to now().
Has anyone done this before

C# equiv of isdate?
baswegan2
Simple Samples
I did that, but all that did was put 1/1/1 in the date. I guess i need someone to tell me what the exact syntax should be, since what i tried either put in a current date, if the date string i passed in is not valid, or it puts in 1/1/1 if i use nocurrentdatedefault.
There's not enough examples in the doc for me to figure out how to use the formatter the way i need.
Richard Wilson
@nt
prawin
It might be overkill to load the VisualBasic assembly for just that one method, but if you find a need for more than a couple of the VisualBasic namespace methods that don't have direct equivalents in C#, there's nothing wrong with using that (extremely) well-tested assembly. There's such a stigma against it, but I've even seen some people recommend using other far more obscure assemblies (shareware, code from someone's blog, etc.) rather than reference the VisualBasic one. For example, the Financial methods are very useful - why reference a third-party tool or write your own if the Financial class has very solid methods tested by far more developers than that third-party tool
Certainly, you shouldn't reference the namespace for things like "Len" or "Left" - these have obvious .NET equivalents. But for more complex methods that don't have non-VisualBasic equivalents, I don't see a problem.
In summary, Microsoft.VisualBasic.dll is just another useful assembly that you have at your disposal. Don't dismiss it just because it has (gasp) "VisualBasic" in it's name.
Also note that Microsoft.VisualBasic.dll is not the VB 'compatibility' dll that you certainly should avoid.
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Clear VB: Cleans up VB code
C# Code Metrics: Quick metrics for C#
JimBobJoe
estyles
This is the code for the IsDate function that come with Microsoft.VisualBasic assembly. I got this code through reflector utility. I know that I am posting this reply after long time.
Jamie Thomson
It is best to use DateTime.TryParse instead of DateTime.Parse if you are just catching and ignoring the exception. Throwing exceptions via DateTime.Parse is a magnitude slower than using DateTime.TryParse, especially if you using this in a loop.
John Siney
Can't you use the VB function:
Microsoft.VisualBasic.isdate()
mallakishore
In ASP.NET, when you use
DateTime.TryParse()
The 2nd parameter is a DateTime output parameter that will be set to the converted date if possible;
if not possible, will be set to DateTime.MinDate;
That's all !!!
Krop
PaulD1
{
string strDate = obj.ToString();
try
{
DateTime dt = DateTime.Parse(strDate);
if(dt != DateTime.MinValue && dt != DateTime.MaxValue)
return true;
return false;
}
catch
{
return false;
}
}
Keith Chapman