Checking a string for valid date

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Day

    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.FormatEx ception occurred in
    mscorlib.dll.



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




  • ocertain

    #2
    RE: Checking a string for valid date

    Dim sDate As String = "3/1/05"
    Dim dtConvDate As Date

    If IsDate(sDate) Then
    dtConvDate = sDate
    End If


    "Bob Day" wrote:
    [color=blue]
    > 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.FormatEx ception occurred in
    > mscorlib.dll.
    >
    >
    >
    > Additional information: the string was not recognized as a valid datetime.
    > There is a unknown word starting at index 0.
    >
    >
    >
    >
    >[/color]

    Comment

    • Samuel R. Neff

      #3
      Re: Checking a string for valid date


      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" <BobDay@TouchTa lk.net>
      wrote:
      [color=blue]
      >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_Vali d = IsDate(x)
      >
      >Should result in False, but throws exception:
      >
      >A first chance exception of the type System.FormatEx ception occurred in
      >mscorlib.dll .
      >
      >
      >
      >Additional information: the string was not recognized as a valid datetime.
      >There is a unknown word starting at index 0.
      >
      >
      >[/color]

      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.

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Checking a string for valid date

        Bob,

        "Bob Day" <BobDay@TouchTa lk.net> schrieb:[color=blue]
        > 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.FormatEx ception occurred in
        > mscorlib.dll.[/color]

        '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/>

        Comment

        • Cor Ligthert

          #5
          Re: Checking a string for valid date

          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


          Comment

          • Bob Day

            #6
            Re: Checking a string for valid date

            Thanks everyone, that all makes perfect sense.

            Bob
            ------
            "Bob Day" <BobDay@TouchTa lk.net> wrote in message
            news:eU2h1NqHFH A.3244@TK2MSFTN GP09.phx.gbl...[color=blue]
            > 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.FormatEx ception occurred in
            > mscorlib.dll.
            >
            >
            >
            > Additional information: the string was not recognized as a valid
            > datetime. There is a unknown word starting at index 0.
            >
            >
            >
            >[/color]


            Comment

            • Oenone

              #7
              Re: Checking a string for valid date

              >> blnDate_Valid = IsDate(x)[color=blue][color=green]
              >> Should result in False, but throws exception:[/color][/color]

              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


              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: Checking a string for valid date

                "Oenone" <oenone@nowhere .com> schrieb:[color=blue][color=green][color=darkred]
                >>> blnDate_Valid = IsDate(x)
                >>> Should result in False, but throws exception:[/color][/color]
                >
                > 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.[/color]

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

                \\\
                ....
                If TypeOf Expression Is String Then
                Try
                Dim time1 As DateTime =
                DateType.FromSt ring(CType(Expr ession,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/>

                Comment

                • Oenone

                  #9
                  Re: Checking a string for valid date

                  Herfried K. Wagner [MVP] wrote:[color=blue]
                  > Mhm... There is nothing special within 'IsDate''s implementation:[/color]
                  [...]

                  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


                  Comment

                  • Cor Ligthert

                    #10
                    Re: Checking a string for valid date

                    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(mydatestr ing)
                    catch ex as error
                    return error
                    end try

                    And a kind of same implementation is for IsNumeric.

                    Cor


                    Comment

                    • Oenone

                      #11
                      Re: Checking a string for valid date

                      Cor Ligthert wrote:[color=blue]
                      > You don't need the source code, it is just a way it is done, this is
                      > the same[/color]

                      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


                      Comment

                      • Herfried K. Wagner [MVP]

                        #12
                        Re: Checking a string for valid date

                        "Oenone" <oenone@nowhere .com> schrieb:[color=blue][color=green]
                        >> Mhm... There is nothing special within 'IsDate''s implementation:[/color]
                        > [...]
                        >
                        > Is the implementation of IsNumeric similar to this?
                        >
                        > (And how did you get to the sourcecode for the IsDate function?)[/color]

                        You can check the implementation yourself:

                        <URL:http://www.aisto.com/roeder/dotnet/Download.aspx?F ile=Reflector.z ip>

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

                        Comment

                        Working...