string to date

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

    #1

    string to date

    This works:

    If remoteFileInfo( arrayIndex).m_s trFileDate < Now.AddDays(-25) Then ....

    but should it???

    remoteFileInfo( arrayIndex).m_s trFileDate is a string for example:
    "9/6/2007 7:00:00 AM"

    Now.AddDays(-25) is a date for example:
    #8/12/2007 10:52:56 AM#

    Shouldn't I be converting remoteFileInfo( arrayIndex).m_s trFileDate to a
    date before compairing?
  • Rory Becker

    #2
    Re: string to date

    This works:
    >
    If remoteFileInfo( arrayIndex).m_s trFileDate < Now.AddDays(-25) Then
    ....
    >
    but should it???
    >
    I'm nopt in a position to test this right now but I would have to guess something
    like.

    You are not using "option Strict on"
    VB.net may be converting your date to a strng for comparison purposes

    This means that you are likely getting lucky for now.

    I would suggest
    ----------------------------------------------------
    If Convert.ToDateT ime(remoteFileI nfo(arrayIndex) .m_strFileDate) < Now.AddDays(-25)
    Then
    ----------------------------------------------------

    --
    Rory


    Comment

    • cj

      #3
      Re: string to date

      Found it.

      If cdate(remoteFil eInfo(arrayInde x).m_strFileDat e < Now.AddDays(-25)

      is what I was looking for.

      P.S. I like option strict off but it was bugging me that I couldn't
      remember how to convert a string to a date. All I could think of was
      stod() which is VFP.



      Rory Becker wrote:
      >This works:
      >>
      >If remoteFileInfo( arrayIndex).m_s trFileDate < Now.AddDays(-25) Then
      >....
      >>
      >but should it???
      >>
      >
      I'm nopt in a position to test this right now but I would have to guess
      something like.
      >
      You are not using "option Strict on" VB.net may be converting your date
      to a strng for comparison purposes
      >
      This means that you are likely getting lucky for now.
      >
      I would suggest
      ----------------------------------------------------
      If Convert.ToDateT ime(remoteFileI nfo(arrayIndex) .m_strFileDate) <
      Now.AddDays(-25) Then
      ----------------------------------------------------
      >
      --
      Rory
      >
      >

      Comment

      • =?ISO-8859-1?Q?G=F6ran_Andersson?=

        #4
        Re: string to date

        cj wrote:
        This works:
        >
        If remoteFileInfo( arrayIndex).m_s trFileDate < Now.AddDays(-25) Then ....
        >
        but should it???
        No, it doesn't work. It might compile, but it doesn't work. The date
        will be converted to a string, and comparing dates as strings doesn't
        work unless you use the ISO 8601 format.
        remoteFileInfo( arrayIndex).m_s trFileDate is a string for example:
        "9/6/2007 7:00:00 AM"
        >
        Now.AddDays(-25) is a date for example:
        #8/12/2007 10:52:56 AM#
        >
        Shouldn't I be converting remoteFileInfo( arrayIndex).m_s trFileDate to a
        date before compairing?
        Yes. Use the Convert.ToDateT ime method.

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • cj

          #5
          Re: string to date

          Yes, it did work. Can't say why, but it did. I was looking for cdate()

          Göran Andersson wrote:
          cj wrote:
          >This works:
          >>
          >If remoteFileInfo( arrayIndex).m_s trFileDate < Now.AddDays(-25) Then ....
          >>
          >but should it???
          >
          No, it doesn't work. It might compile, but it doesn't work. The date
          will be converted to a string, and comparing dates as strings doesn't
          work unless you use the ISO 8601 format.
          >
          >remoteFileInfo (arrayIndex).m_ strFileDate is a string for example:
          >"9/6/2007 7:00:00 AM"
          >>
          >Now.AddDays(-25) is a date for example:
          >#8/12/2007 10:52:56 AM#
          >>
          >Shouldn't I be converting remoteFileInfo( arrayIndex).m_s trFileDate to
          >a date before compairing?
          >
          Yes. Use the Convert.ToDateT ime method.
          >

          Comment

          • =?ISO-8859-1?Q?G=F6ran_Andersson?=

            #6
            Re: string to date

            cj wrote:
            Yes, it did work. Can't say why, but it did.
            I see. Yes, actually it does work in this case. For some reason it
            converts the string to a date, not the date to a string.

            Relying on that the compiler manages to correctly guess what you mean is
            not a good practice, though. You should use "Option Script On" so that
            you don't do conversions like this by mistake.

            If you don't know why something works, you can't know if it's actually
            working correctly for all possible cases, or if it's going to work in
            the future. Just because something looks like it works doesn't
            automatically mean that it's correct. I've seen code that has been used
            for years, that suddenly failed just because it was wrong from the
            beginning.

            --
            Göran Andersson
            _____
            Göran Anderssons privata hemsida.

            Comment

            • cj

              #7
              Re: string to date



              Göran Andersson wrote:
              cj wrote:
              >Yes, it did work. Can't say why, but it did.
              >
              I see. Yes, actually it does work in this case. For some reason it
              converts the string to a date, not the date to a string.
              >
              Relying on that the compiler manages to correctly guess what you mean is
              not a good practice, though. You should use "Option Script On" so that
              you don't do conversions like this by mistake.
              IYHO of course :)
              >
              If you don't know why something works, you can't know if it's actually
              working correctly for all possible cases, or if it's going to work in
              the future. Just because something looks like it works doesn't
              automatically mean that it's correct. I've seen code that has been used
              for years, that suddenly failed just because it was wrong from the
              beginning.
              >

              Comment

              • Rory Becker

                #8
                Re: string to date

                cj wrote:
                >>
                >Relying on that the compiler manages to correctly guess what you mean
                >is not a good practice, though. You should use "Option Script On" so
                >that you don't do conversions like this by mistake.
                >>
                IYHO of course :)
                I'd *think* you'll find that *most* people (I wonder how much trouble I can
                get in for using that phrase wthout actually asking most people ;P) would
                agree with Goran.

                Codeing VB.net without "Option Strict On" as a *default* stance can be much
                like the difference between coding VBA and Coding VB6

                If there is a possibility that the compiler could get it wrong, then you
                shouldn't leave it up to the compiler. you should tell the compiler explicitly.
                "Option Strict on" is a great way to get the compiler to tell you where the
                uncertainty is.

                That said there are places where the reverse is true,. It's just that there
                are not many so IMHO (:)) the deafult should be "Option strict On"




                --
                Rory


                Comment

                • rowe_newsgroups

                  #9
                  Re: string to date

                  I'd *think* you'll find that *most* people (I wonder how much trouble I can
                  get in for using that phrase wthout actually asking most people ;P) would
                  agree with Goran.
                  Don't worry Rory - if you, Goran, and I all agree about Option Strict
                  On then it has to be right. After all we're know better than *most*
                  people (hehe just kidding - no flames please :-))

                  It's true though - I see no reason why any developer would leave this
                  off, unless they have to use late-binding (though I believe you could
                  just suppress the message). The annoyance of having to explicitly cast/
                  convert all your types is nothing compared to having to explain to a
                  client that that phantom crash they keep having is because of you
                  forgot to cast a string into a integer before multiplying. Besides, if
                  you ever plan on using a different language (say C#) you need to get
                  used to explicitly declaring everything, as C# is more picky than VB
                  with Option Strict On.

                  After all, the old saying is "Never put off until runtime what you can
                  fix at compile time."

                  Or something like that...

                  Thanks,

                  Seth Rowe


                  Comment

                  Working...