473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Checking a string for valid date

The IsDate code below should result in False, instead it throws the
exception below. Why? How do I check if a string can be converted to a
date if this function does not work properly?

Bob

code:
Dim blnDate_Valid As Boolean = True
Dim x As String = "Hello"

blnDate_Valid = IsDate(x)

Should result in False, but throws exception:

A first chance exception of the type System.FormatException occurred in
mscorlib.dll.

Additional information: the string was not recognized as a valid datetime.
There is a unknown word starting at index 0.


Nov 21 '05 #1
11 33281
Dim sDate As String = "3/1/05"
Dim dtConvDate As Date

If IsDate(sDate) Then
dtConvDate = sDate
End If
"Bob Day" wrote:
The IsDate code below should result in False, instead it throws the
exception below. Why? How do I check if a string can be converted to a
date if this function does not work properly?

Bob

code:
Dim blnDate_Valid As Boolean = True
Dim x As String = "Hello"

blnDate_Valid = IsDate(x)

Should result in False, but throws exception:

A first chance exception of the type System.FormatException occurred in
mscorlib.dll.

Additional information: the string was not recognized as a valid datetime.
There is a unknown word starting at index 0.


Nov 21 '05 #2

If you have the IDE set to break on all exceptions and not just
unhandled exceptions then it will break on the exception thrown
internally by the isDate function that it uses to determine if a date
is valid.

Change the IDE setting under Debug > Exceptions...

HTH,

Sam

On Tue, 1 Mar 2005 16:13:27 -0500, "Bob Day" <Bo****@TouchTalk.net>
wrote:
The IsDate code below should result in False, instead it throws the
exception below. Why? How do I check if a string can be converted to a
date if this function does not work properly?

Bob

code:
Dim blnDate_Valid As Boolean = True
Dim x As String = "Hello"

blnDate_Valid = IsDate(x)

Should result in False, but throws exception:

A first chance exception of the type System.FormatException occurred in
mscorlib.dll.

Additional information: the string was not recognized as a valid datetime.
There is a unknown word starting at index 0.


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 21 '05 #3
Bob,

"Bob Day" <Bo****@TouchTalk.net> schrieb:
The IsDate code below should result in False, instead it throws the
exception below. Why? How do I check if a string can be converted to a
date if this function does not work properly?
[...]
Dim blnDate_Valid As Boolean = True
Dim x As String = "Hello"

blnDate_Valid = IsDate(x)

Should result in False, but throws exception:

A first chance exception of the type System.FormatException occurred in
mscorlib.dll.


'IsDate' internally throws an exception if the string cannot be parsed, but
this exception is caught by 'IsDate'. Nevertheless, the exception is shown
if you configured the IDE to stop whenever an exception is thrown. You can
change this behavior by choosing "Debug" -> "Exceptions..." -> "Common
Language Runtime Exceptions" -> "When the exception is thrown:" -> (o)
"Continue".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
Bob

An answer on your Why?

This is because that internally by Microsoft is used a
Try
Catch

To find the errors.

I hope this helps?

Cor
Nov 21 '05 #5
Thanks everyone, that all makes perfect sense.

Bob
------
"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:eU**************@TK2MSFTNGP09.phx.gbl...
The IsDate code below should result in False, instead it throws the
exception below. Why? How do I check if a string can be converted to a
date if this function does not work properly?

Bob

code:
Dim blnDate_Valid As Boolean = True
Dim x As String = "Hello"

blnDate_Valid = IsDate(x)

Should result in False, but throws exception:

A first chance exception of the type System.FormatException occurred in
mscorlib.dll.

Additional information: the string was not recognized as a valid
datetime. There is a unknown word starting at index 0.

Nov 21 '05 #6
>> blnDate_Valid = IsDate(x)
Should result in False, but throws exception:


Actually, I still think this is an error in VB.NET. I don't understand why
IsDate allows an internally-thrown exception to be caught by the procedure
that calls it. For example, consider the following code:

Dim s As String
s = "Blah"
Debug.WriteLine(IsNumeric(s))
Debug.WriteLine(IsDate(s))

If you run that with the IDE set to break on CLR exceptions, it successfully
processes the IsNumeric() call without any problems, but then breaks into
the IDE on the IsDate() call. This seems inconsistent to me.

The exception can't even be caught by the calling procedure. For example:

Try
Debug.WriteLine(IsDate(s))
Catch ex As Exception
Debug.WriteLine("Error: " & ex.Message)
End Try

Run this with the IDE set NOT to break and you'll find that the exception is
not caught. So there's no reason to allow the IDE to know that an exception
occurred.

IsDate() is the only function in the entire language runtime that I've found
that exhibits this behaviour.

It's also darned annoying. :) I like to run my code with the IDE always set
to break on exceptions as I find it much easier to track problems down if I
can immediately see where the exception occurred. I've had to write a
wrapper around IsDate() that performs some basic validation (not an empty
string, first character is numeric, etc.) before it calls into IsDate in an
attempt to weed out as many non-date values as I can. :-/

Hopefully this will change in VS2005.

--

(O) e n o n e
Nov 21 '05 #7
"Oenone" <oe****@nowhere.com> schrieb:
blnDate_Valid = IsDate(x)
Should result in False, but throws exception:


Actually, I still think this is an error in VB.NET. I don't understand why
IsDate allows an internally-thrown exception to be caught by the procedure
that calls it. For example, consider the following code:

Dim s As String
s = "Blah"
Debug.WriteLine(IsNumeric(s))
Debug.WriteLine(IsDate(s))

If you run that with the IDE set to break on CLR exceptions, it
successfully
processes the IsNumeric() call without any problems, but then breaks into
the IDE on the IsDate() call. This seems inconsistent to me.


Mhm... There is nothing special within 'IsDate''s implementation:

\\\
....
If TypeOf Expression Is String Then
Try
Dim time1 As DateTime =
DateType.FromString(CType(Expression,String))
Return True
Catch exception1 As Exception
End Try
End If
....
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8
Herfried K. Wagner [MVP] wrote:
Mhm... There is nothing special within 'IsDate''s implementation:

[...]

Is the implementation of IsNumeric similar to this?

(And how did you get to the sourcecode for the IsDate function?)

--

(O) e n o n e
Nov 21 '05 #9
Oenone,

You don't need the source code, it is just a way it is done, this is the
same

try
dt as datetime = CDate(mydatestring)
catch ex as error
return error
end try

And a kind of same implementation is for IsNumeric.

Cor
Nov 21 '05 #10
Cor Ligthert wrote:
You don't need the source code, it is just a way it is done, this is
the same


I know that, but Herfried was posting from the actual VB IsDate() function.

I've found how to access it myself now (using the DotNet Reflector
application at http://www.aisto.com/roeder/dotnet/). It's very interesting
to see how the functions are working internally. And IsNumeric() is quite a
lot more complex than IsDate()...

--

(O) e n o n e
Nov 21 '05 #11
"Oenone" <oe****@nowhere.com> schrieb:
Mhm... There is nothing special within 'IsDate''s implementation:

[...]

Is the implementation of IsNumeric similar to this?

(And how did you get to the sourcecode for the IsDate function?)


You can check the implementation yourself:

<URL:http://www.aisto.com/roeder/dotnet/Download.aspx?File=Reflector.zip>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: SQLScott | last post by:
I have created a VB.Net app that takes data from Visual Foxpro and inserts it into SQL Server. The problem I am running into is that I have come across records in Foxpro where the dates are as...
28
by: romy | last post by:
What's the easiest way to verify the user had entered a valid date ?
5
by: Takeadoe | last post by:
Gang - I'm generating date and time variables from scanned forms. Currently, the date and time values are as follows: 06/26/2006 and 11:30 AM. I've written VBA code to combine them into a...
4
by: sang | last post by:
How can I convert a string like '11-oct-2006' into a valid mysql date? the date_format doesnot change the string in requried date format. create table sample(name varchar(20),date varchar(16));...
1
by: pankajprakash | last post by:
Hi All, I Made a window application in c#.net and trying to fetch the record from the postgresql database. But whever i run the application "String was not recognized as a Valid Date Time" message...
2
by: pvenkatesh2k4 | last post by:
hi to all i need to convert string to date. it shows invalid string format for parsing here is my code: string s="13/07/07" //in dd/mm/yy format DateTime d=DateTime.Parse(s) //actually system...
2
by: RN1 | last post by:
A TextBox displays the current date (in dd/mm/yyyy format) & time when a user comes to a page (e.g. 15/10/2008 1:36:39 PM). To convert the date into international format so that the remote server...
2
by: raj0390 | last post by:
hi , i am unable to convert'Apr 19 20:00:03 CDT 2009' which is a string to a valid date format plz let me know the function
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.