Date Function

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

Hello, VB has an IsDate function which determines if a text field is a
valid date. What is the equivalent in C#?

Thanks in advance for your assistance!!!!!!
 
try
{
DateTime dt = DateTime.Parse(stringDate);
}
catch (FormatException e)
{
// Invalid date string
}

-vJ
 
You could use the System.DateTime.Parse() method and check for a
FormatException:

public bool IsDate(string testString) {
try {
DateTime dummy = DateTime.Parse(string);
return true;
}
catch (FormatException) {
return false;
}
}
 
I'd personally use the method Vijay and Ed suggest, but just as an FYI, you
can use Microsoft.VisualBasic.Information.IsDate(textBox1.text);
http://www.netcoole.com/asp2aspx/vbhtml/csfuncs.htm

You'll need to add a reference to Micorosft.VisualBasic.Information (AFAIK,
I think you need to actually add the reference not just import the
namespace.
 
you could try the System.Convert.ToDateTime(dateString) method.
if a FormatException is thrown then the string is not a valid date.
i don't know of any specific keyword in C# that validates dates.
hope this helps.
sharon.
 
Back
Top