Home All Groups Group Topic Archive Search About

IsDate("ISomeTimesHateProgrammingMarch2005") = True

Author
30 Jan 2006 5:18 PM
MDC
Why does this return true:

IsDate("ISometimesHateProgrammingMarch2005")

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

Thanks in advance!

MDC

Author
30 Jan 2006 5:35 PM
Patrice
Works as expected here...

--

Show quoteHide quote
"MDC" <michaeldavid***@gmail.com> a écrit dans le message de
news:1138641508.371212.311020@z14g2000cwz.googlegroups.com...
> Why does this return true:
>
> IsDate("ISometimesHateProgrammingMarch2005")
>
> Is there another way to verify that this is not a date??
>
> Thanks in advance!
>
> MDC
>
Author
30 Jan 2006 6:12 PM
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
Author
30 Jan 2006 7:05 PM
Patrice
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

Show quoteHide quote
"MDC" <michaeldavid***@gmail.com> a écrit dans le message de
news:1138644773.728314.60220@g43g2000cwa.googlegroups.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
>
Author
30 Jan 2006 5:42 PM
Kerry Moorman
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


Show quoteHide quote
"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
>
>
Author
30 Jan 2006 6:37 PM
Herfried K. Wagner [MVP]
"MDC" <michaeldavid***@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/>
Author
30 Jan 2006 7:50 PM
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.
Author
30 Jan 2006 8:09 PM
Herfried K. Wagner [MVP]
"MDC" <michaeldavid***@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/>
Author
6 Feb 2006 2:41 AM
Jay B. Harlow [MVP - Outlook]
| '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/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatetimeclassparseexacttopic3.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


Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:uIRdFkdJGHA.604@TK2MSFTNGP14.phx.gbl...
| "MDC" <michaeldavid***@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/>
|
Author
30 Jan 2006 9:13 PM
Kerry Moorman
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


Show quoteHide quote
"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.
>
>
Author
30 Jan 2006 6:44 PM
Mike
"MDC" <michaeldavid***@gmail.com> wrote in message
news:1138641508.371212.311020@z14g2000cwz.googlegroups.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.
Author
30 Jan 2006 7:51 PM
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
Author
31 Jan 2006 10:56 AM
Larry Lard
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
Author
31 Jan 2006 11:11 AM
Cor Ligthert [MVP]
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
Author
31 Jan 2006 2:49 PM
_AnonCoward
"MDC" <michaeldavid***@gmail.com> wrote in message
news:1138641508.371212.311020@z14g2000cwz.googlegroups.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.
Author
7 Feb 2006 3:51 AM
Jim Wooley
Show quote Hide quote
> "MDC" <michaeldavid***@gmail.com> wrote in message
> news:1138641508.371212.311020@z14g2000cwz.googlegroups.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)
Author
7 Feb 2006 6:02 AM
Cor Ligthert [MVP]
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
Author
9 Feb 2006 4:21 AM
Jim Wooley
> 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
Author
6 Feb 2006 2:56 AM
Jay B. Harlow [MVP - Outlook]
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.DateTimeFormat.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/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatetimeclassparseexacttopic.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.CreateSpecificCulture("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


Show quoteHide quote
"MDC" <michaeldavid***@gmail.com> wrote in message
news:1138641508.371212.311020@z14g2000cwz.googlegroups.com...
| Why does this return true:
|
| IsDate("ISometimesHateProgrammingMarch2005")
|
| Is there another way to verify that this is not a date??
|
| Thanks in advance!
|
| MDC
|
Author
6 Feb 2006 3:33 AM
Jay B. Harlow [MVP - Outlook]
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.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


The:

|            culture =
| System.Globalization.CultureInfo.CreateSpecificCulture("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]" <Jay_Harlow_***@tsbradley.net> wrote in
message news:uYog4jsKGHA.2012@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.DateTimeFormat.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/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatetimeclassparseexacttopic.asp
Show quoteHide quote
|
| 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.CreateSpecificCulture("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" <michaeldavid***@gmail.com> wrote in message
| news:1138641508.371212.311020@z14g2000cwz.googlegroups.com...
|| Why does this return true:
||
|| IsDate("ISometimesHateProgrammingMarch2005")
||
|| Is there another way to verify that this is not a date??
||
|| Thanks in advance!
||
|| MDC
||
|
|