Jump to content
Xtreme .Net Talk

String not recognized as valid DateTime in IsDate funtion


Recommended Posts

Posted

When executing code to determine if a text box contains text that's convertable to a date I get the following error :

 

 

 

A first chance exception of type 'System.FormatExeption' occured in mscorlib.dl.

 

Additional information : String was not recognized as a valid DateTime.

 

 

This happens when executing the code :

 

If IsDate(txtDate.Text) Then .....

 

Oddly enough, when I break the program and run this command in the immediate window the command comes back with a boolean as I expect.

 

Anyone have any ideas?

 

Thanks

 

James

  • *Gurus*
Posted
Actually, my above statements assumed too much. I attempted to input null (Nothing) and empty (String.Empty) values and the function still returned correctly. I have a feeling that your problem is elsewhere, possibly in that surrounding block of code. If you could post the remainder of your code I'd like to look it over.
Posted (edited)

Not much to show, but here it is :

 

   Private Sub txtPullDate_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPullDate.TextChanged
   'Actions to take when the txtPullDate box changes value
If IsDate(txtPullDate.Text.Trim) Then
 txtPullDate.Text = "The If statment is busted!"

End If
   '---
     End Sub

Edited by divil
  • *Experts*
Posted

Where is this IsDate function? The only one I could find in the

object browser was in the MS compat. library, in with the likes of

'UBound' et al.

 

My IsDate function looks something like this:

Public Function IsDate(ByVal d As String)
 Try
   Dim dummy As Date = Date.Parse(d)
   Return True
 Catch
   Return False
 End Try
End Function

  • *Experts*
Posted

It's in the 'Visual Basic Language Reference' portion of the MSDN,

along with UBound, QBColor, RGB and others.

I don't think it's a .NET function at all.

  • *Experts*
Posted

Ah, I see what you mean now. D'oh! Still, is it not better to use a

self-written function over a Microsoft-compat. library function, since

they can only be accessed (easily) from within VB, whereas that

function could be used in C# if it was required?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...