By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,822 Members | 731 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,822 IT Pros & Developers. It's quick & easy.

IsDate("ISomeTimesHateProgrammingMarch2005") = True

P: n/a
MDC
Why does this return true:

IsDate("ISometimesHateProgrammingMarch2005")

Is there another way to verify that this is not a date??

Thanks in advance!

MDC

Jan 30 '06 #1
Share this Question
Share on Google+
19 Replies


P: n/a
Works as expected here...

--

"MDC" <mi*************@gmail.com> a écrit dans le message de
news:11**********************@z14g2000cwz.googlegr oups.com...
Why does this return true:

IsDate("ISometimesHateProgrammingMarch2005")

Is there another way to verify that this is not a date??

Thanks in advance!

MDC

Jan 30 '06 #2

P: n/a
MDC,

I suspect that IsDate is using DateTime.Parse, which also says that your
example input is a date.

Another option is to use DateTime.ParseExact.

Kerry Moorman
"MDC" wrote:
Why does this return true:

IsDate("ISometimesHateProgrammingMarch2005")

Is there another way to verify that this is not a date??

Thanks in advance!

MDC

Jan 30 '06 #3

P: n/a
MDC
Patrice wrote:
Works as expected here...

Really? It returns FALSE? What framework are you using? I am using
v1.1.4322 (VS .NET 2003).

Thanks.

MDC

Jan 30 '06 #4

P: n/a
"MDC" <mi*************@gmail.com> schrieb:
Why does this return true:

IsDate("ISometimesHateProgrammingMarch2005")

Is there another way to verify that this is not a date??


What does 'CDate("IsSome...2005") return?

You may want to use 'DateTime.Parse'/'DateTime.ParseExact' + exception
handling to check if a date can be parsed.

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

Jan 30 '06 #5

P: n/a
"MDC" <mi*************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Why does this return true:

IsDate("ISometimesHateProgrammingMarch2005")

Is there another way to verify that this is not a date??


Strange that your system believes this is a date. Try using another function
on it, such as DateAdd; and catching any errors.

Jan 30 '06 #6

P: n/a
Well that is unless I use the month name in my local language ;-)

So it looks like that they consider that if the string ends (and perhaps
starts or contains) with some recognizable date, then they'll convert this
to a date.
This is likely for compatibility with behavior of IsDate/CDate in earlier VB
version.

See the other threads for .NET specific behavior...

--
Patrice

"MDC" <mi*************@gmail.com> a écrit dans le message de
news:11*********************@g43g2000cwa.googlegro ups.com...
Patrice wrote:
Works as expected here...

Really? It returns FALSE? What framework are you using? I am using
v1.1.4322 (VS .NET 2003).

Thanks.

MDC

Jan 30 '06 #7

P: n/a
MDC
CDate returns 3/1/2005 as does .Parse.

Herfried and Kerry, I have not tried .ParseExact because I am not sure
we can use it as the date string can be in a wide variety of formats
(long, short, general). I am not a Date guru so I am slowly trying
everything I can.

Thanks.

Jan 30 '06 #8

P: n/a
MDC
>Strange that your system believes this is a date. Try using another function on it, such as
DateAdd; and catching any errors.

Mike, I will give that a try, also.

Thanks.

MDC

Jan 30 '06 #9

P: n/a
"MDC" <mi*************@gmail.com> schrieb:
CDate returns 3/1/2005 as does .Parse.
.... which is why 'IsDate' returns 'True'. The string can be parsed into a
'Date'.
Herfried and Kerry, I have not tried .ParseExact because I am not sure
we can use it as the date string can be in a wide variety of formats
(long, short, general).


'ParseExact' is used if the string should only be matched against a single
date pattern.

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

Jan 30 '06 #10

P: n/a
MDC,

Although I have not tried it, I think that one of the overloads to
ParseExact accepts an array of date formats to use during the parse.

Kerry Moorman
"MDC" wrote:
CDate returns 3/1/2005 as does .Parse.

Herfried and Kerry, I have not tried .ParseExact because I am not sure
we can use it as the date string can be in a wide variety of formats
(long, short, general). I am not a Date guru so I am slowly trying
everything I can.

Thanks.

Jan 30 '06 #11

P: n/a

MDC wrote:
Why does this return true:

IsDate("ISometimesHateProgrammingMarch2005")


You are (to my mind) quite right to be puzzled. This string isn't
regarded as a date by the VB6 IsDate function - correctly, since it
*obviously isn't a date* - so the real mystery is what bizarro code is
running in the framework to claim that it is a date. This is clearly a
bug.

Mind you, there are plenty of cases where a .NET method named 'some
English phrase' has a meaning or effect completely different than the
meaning of that English phrase, so maybe this is some kind of design
theme.

It seems unfortunate that we now have to use ParseExact, and consider
every possible permutation of date format that might be supplied, in
order to try and convert an arbitrary string into a date - whereas in
the Stone Age of VB6, we had to - use a single builtin function...

--
Larry Lard
Replies to group please

Jan 31 '06 #12

P: n/a
Hi MDC,

If IsDate("ISometimesHateProgrammingMaart2005") = False Then
MessageBox.Show("false")

Gives for me in version 2005 "false" and nothing in version 2003

And because of that you can say in my opinion that it is a bug in version
2003.

Be aware that IsDate as least in version 2003 is nothing more than a try
Catch block with a DateTime.Parse (or convert.ToDateTime). It is just
something shorter to use.

I hope this helps,

Cor
Jan 31 '06 #13

P: n/a

"MDC" <mi*************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
:
: Why does this return true:
:
: IsDate("ISometimesHateProgrammingMarch2005")
:
: Is there another way to verify that this is not a date??
:
: Thanks in advance!
:
: MDC
Interesting. I created the following code and saved to file v.vb:

'-------------------------------------
Imports Microsoft.VisualBasic
Imports System

Public Module [module]

Public Sub Main
msgbox(isdate("ISometimesHateProgrammingMarch2005" ))
End Sub

End Module

'-------------------------------------

I opened two command windows, one with a path pointing to the 1.1 version of
the vbc compiler and another pointing to the 2.0 version. I then complied
the file (vbc v.vb) and ran it from the command line.

When I ran the 1.1 compiled executable, the msgbox displayed 'true' whereas
the 2.0 compiled executable displayed 'false'.

I checked the IL code for each version and I didn't see anything
meaningfully different. Evidently the 2.0 IsDate function has been updated
in version 2.0
Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Jan 31 '06 #14

P: n/a
| 'ParseExact' is used if the string should only be matched against a single
| date pattern.
As Kerry suggests ParseExact is overloaded to accept an array of date
formats.

http://msdn.microsoft.com/library/de...xacttopic3.asp

Using the above overload you could send in long, short, general date
formats...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uI*************@TK2MSFTNGP14.phx.gbl...
| "MDC" <mi*************@gmail.com> schrieb:
| > CDate returns 3/1/2005 as does .Parse.
|
| ... which is why 'IsDate' returns 'True'. The string can be parsed into a
| 'Date'.
|
| > Herfried and Kerry, I have not tried .ParseExact because I am not sure
| > we can use it as the date string can be in a wide variety of formats
| > (long, short, general).
|
| 'ParseExact' is used if the string should only be matched against a single
| date pattern.
|
| --
| M S Herfried K. Wagner
| M V P <URL:http://dotnet.mvps.org/>
| V B <URL:http://classicvb.org/petition/>
|
Feb 6 '06 #15

P: n/a
MDC,
In addition to the other comments. If you are using VS 2005 (.NET 2.0) , I
would consider using the overload of DateTime.TryParseExact that accepts
multiple formats:

http://msdn2.microsoft.com/h9b85w22.aspx

Public Function IsDate(ByVal expression As String) As Boolean
Dim result As DateTime
Dim formats() As String =
System.Globalization.CultureInfo.CurrentCulture.Da teTimeFormat.GetAllDateTimePatterns()
Return DateTime.TryParseExact(expression, formats,
System.Globalization.CultureInfo.CurrentCulture,
Globalization.DateTimeStyles.AllowWhiteSpaces, result)
End Function

Use the result variable above if you want the converted date.
If upgrading to VS 2005 is not an option, .NET 1.x has a DateTime.ParseExact
that accepts multiple formats:

http://msdn.microsoft.com/library/de...exacttopic.asp

You could wrap the call in a Try Catch to return the Boolean value.

Public Function IsDate(ByVal expression As String) As Boolean
Try
Dim culture As System.Globalization.CultureInfo =
System.Globalization.CultureInfo.CurrentCulture
culture =
System.Globalization.CultureInfo.CreateSpecificCul ture("de-DE")
Dim formats() As String =
culture.DateTimeFormat.GetAllDateTimePatterns()
Dim result As DateTime = DateTime.ParseExact(expression,
formats, System.Globalization.CultureInfo.CurrentCulture,
Globalization.DateTimeStyles.AllowWhiteSpaces)
Catch ex As Exception
Return False
End Try
End Function

Of course if you want to limit the support formats you could create an array
of formats that you support instead of
DateTimeFormat.GetAllDateTimePatterns...
Be certain to read the effects of varying
System.Globalization.DateTimeStyles values have on the call to
DateTime.ParseExact & DateTime.TryParseExact.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"MDC" <mi*************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
| Why does this return true:
|
| IsDate("ISometimesHateProgrammingMarch2005")
|
| Is there another way to verify that this is not a date??
|
| Thanks in advance!
|
| MDC
|
Feb 6 '06 #16

P: n/a
Doh!
The .NET 1.x routine should be more like:

Public Function IsDate(ByVal expression As String) As Boolean
Try
Dim formats() As String =
System.Globalization.CultureInfo.CurrentCulture.Da teTimeFormat.GetAllDateTimePatterns()
Dim result As DateTime = DateTime.ParseExact(expression,
formats, System.Globalization.CultureInfo.CurrentCulture,
Globalization.DateTimeStyles.AllowWhiteSpaces)
Catch ex As Exception
Return False
End Try
End Function
The:

| culture =
| System.Globalization.CultureInfo.CreateSpecificCul ture("de-DE")

was verifying other cultures returned a different set of formats.
Technically I should have passed the same culture to the DateTime.ParseExact
method!

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:uY**************@TK2MSFTNGP14.phx.gbl...
| MDC,
| In addition to the other comments. If you are using VS 2005 (.NET 2.0) , I
| would consider using the overload of DateTime.TryParseExact that accepts
| multiple formats:
|
| http://msdn2.microsoft.com/h9b85w22.aspx
|
| Public Function IsDate(ByVal expression As String) As Boolean
| Dim result As DateTime
| Dim formats() As String =
|
System.Globalization.CultureInfo.CurrentCulture.Da teTimeFormat.GetAllDateTimePatterns()
| Return DateTime.TryParseExact(expression, formats,
| System.Globalization.CultureInfo.CurrentCulture,
| Globalization.DateTimeStyles.AllowWhiteSpaces, result)
| End Function
|
| Use the result variable above if you want the converted date.
|
|
| If upgrading to VS 2005 is not an option, .NET 1.x has a
DateTime.ParseExact
| that accepts multiple formats:
|
|
http://msdn.microsoft.com/library/de...exacttopic.asp
|
| You could wrap the call in a Try Catch to return the Boolean value.
|
| Public Function IsDate(ByVal expression As String) As Boolean
| Try
| Dim culture As System.Globalization.CultureInfo =
| System.Globalization.CultureInfo.CurrentCulture
| culture =
| System.Globalization.CultureInfo.CreateSpecificCul ture("de-DE")
| Dim formats() As String =
| culture.DateTimeFormat.GetAllDateTimePatterns()
| Dim result As DateTime = DateTime.ParseExact(expression,
| formats, System.Globalization.CultureInfo.CurrentCulture,
| Globalization.DateTimeStyles.AllowWhiteSpaces)
| Catch ex As Exception
| Return False
| End Try
| End Function
|
| Of course if you want to limit the support formats you could create an
array
| of formats that you support instead of
| DateTimeFormat.GetAllDateTimePatterns...
|
|
| Be certain to read the effects of varying
| System.Globalization.DateTimeStyles values have on the call to
| DateTime.ParseExact & DateTime.TryParseExact.
|
| --
| Hope this helps
| Jay [MVP - Outlook]
| .NET Application Architect, Enthusiast, & Evangelist
| T.S. Bradley - http://www.tsbradley.net
|
|
| "MDC" <mi*************@gmail.com> wrote in message
| news:11**********************@z14g2000cwz.googlegr oups.com...
|| Why does this return true:
||
|| IsDate("ISometimesHateProgrammingMarch2005")
||
|| Is there another way to verify that this is not a date??
||
|| Thanks in advance!
||
|| MDC
||
|
|
Feb 6 '06 #17

P: n/a
> "MDC" <mi*************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
:
: Why does this return true:
:
: IsDate("ISometimesHateProgrammingMarch2005")
:
: Is there another way to verify that this is not a date??
:
: Thanks in advance!
:
: MDC
Interesting. I created the following code and saved to file v.vb:

'-------------------------------------
Imports Microsoft.VisualBasic
Imports System
Public Module [module]

Public Sub Main
msgbox(isdate("ISometimesHateProgrammingMarch2005" ))
End Sub
End Module

'-------------------------------------

I opened two command windows, one with a path pointing to the 1.1
version of the vbc compiler and another pointing to the 2.0 version. I
then complied the file (vbc v.vb) and ran it from the command line.

When I ran the 1.1 compiled executable, the msgbox displayed 'true'
whereas the 2.0 compiled executable displayed 'false'.

I checked the IL code for each version and I didn't see anything
meaningfully different. Evidently the 2.0 IsDate function has been
updated in version 2.0


As as been stated earlier, the 1.x version of IsDate us simply a wrapper
for Date.Parse with a try..catch block.

The Microsoft.VisualBasic.Information.IsDate function includes the following
code:

Try
Dim time1 As DateTime =
DateType.FromString(CType(Expression, String))
Return True
Catch exception1 As Exception
End Try
Return False

In 2.0, this has been modified to this:

If (Not text1 Is Nothing) Then
Dim time1 As DateTime
Return Conversions.TryParseDate _
(text1, time1)
End If

Drilling into the TryParseDate method, the real work is being done in System.DateTimeParse.TryParse.
(Too much code to post here on that one). Long story short, instead of using
IsDate, the 2.0 prefered way of checking for a date value in 2.0 is to use
DateTime.TryParse(object, retValue). I've made some performance comparisons
between IsDate and Date.TryParse and find that Date.TryParse consistently
outperforms the other options. In fact, I've put together a test rig for
a meeting I'm doing Wednesday. It should be posted at www.avbsg.net within
the week. FWIW, I get the following results when trying your sample string.
Using 10000 iterations of testing for a valid type and setting a local date
variable to the result if it validates:

Using IsDate: 6150 ticks
Using Date.Parse inside a Try..Catch block (the method used in .net 1.x):
265569 ticks
using Date.TryParse: 2238 ticks

All of the 2.0 tests result in the string not validating to a valid date.

for comparison, testing a valid date over 10000 iterations results in the
following:

Using IsDate: 19857 ticks
using Date.Parse inside a Try..Catch block: 8668 ticks
Using date.TryParse: 8550 ticks

My take on this, Date.Parse is acceptable only if an invalid date is entered
as an exceptional condition. The additional time use when supplied with a
valid value is due to the fact that I am not only testing the validity of
the type, but also assigning it to a variable. If the validity test fails,
the assignment is never made. In either case, Date.TryParse outperforms the
alternatives and should be used when given the opportunity.

Jim Wooley

(Thanks to Reflector for the .Net framework disassembly)
Feb 7 '06 #18

P: n/a
Jim,

Nicely done, as comment from me because I miss that part.

IsDate is nicely for everybody, even if he does not know anything from
programming, to understand code. Conversion.TryParse.Date is in my opinion
oposite for that. Therefore I would use IsDate. (In a standard program, if
we become in environments where a part of a millisecond becomes real
important than it is for me as well different of course). However the last
is never the situation as by instance the date is entered by the user.

Just my opinion,

Cor
Feb 7 '06 #19

P: n/a
> outperforms the other options. In fact, I've put together a test rig
for

a meeting I'm doing Wednesday. It should be posted at www.avbsg.net
within

the week.


Update: The sample project with slides is available at http://www.avbsg.net/Uploads/TryParse.zip.

Jim Wooley
Feb 9 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.