Date assembly

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

    #1

    Date assembly

    I have data stored in an array in this format:
    Str(0) = "AUG"
    Str(1) = "10"
    Str(2) = "13:44:38" (24 Clock)

    I need to re-assemble this into a valid date variable. What is the
    best algorythm to do this?

    Right now I'm converting AUG to an 8 and then 10 to a day and assuming
    that the year is always 2005.

    8/10/2005 13:44:38

    There has to be a better way...?

  • Herfried K. Wagner [MVP]

    #2
    Re: Date assembly

    "pmclinn" <pmclinn@gmail. com> schrieb:[color=blue]
    >I have data stored in an array in this format:
    > Str(0) = "AUG"
    > Str(1) = "10"
    > Str(2) = "13:44:38" (24 Clock)
    >
    > I need to re-assemble this into a valid date variable. What is the
    > best algorythm to do this?
    >
    > Right now I'm converting AUG to an 8 and then 10 to a day and assuming
    > that the year is always 2005.[/color]

    \\\
    Dim str(2) As String
    str(0) = "AUG"
    str(1) = "10"
    str(2) = "13:44:38"
    MsgBox( _
    Date.ParseExact ( _
    str(1) & " " & str(0) & " 2005 " & str(2), _
    "dd MMM yyyy HH:mm:ss", _
    Nothing _
    ) _
    )
    ///

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

    Comment

    • 

      #3
      Re: Date assembly

      perhaps this would do what you want (you assume what you will about the
      year):

      Dim dateSegments As String() = {"AUG", "10", "13:44:38"}

      ReDim Preserve dateSegments(da teSegments.Leng th)

      dateSegments(da teSegments.GetU pperBound(0)) = Now.ToString("y yyy")

      Dim derivedDate As Date = CDate(String.Fo rmat("{0} {1}, {3} {2}",
      dateSegments))

      Console.Write(d erivedDate.ToSt ring("MM/dd/yyyy HH:mm:ss"))


      Comment

      • Ross Presser

        #4
        Re: Date assembly

        On 11 Aug 2005 10:25:51 -0700, pmclinn wrote:
        [color=blue]
        > I have data stored in an array in this format:
        > Str(0) = "AUG"
        > Str(1) = "10"
        > Str(2) = "13:44:38" (24 Clock)
        >
        > I need to re-assemble this into a valid date variable. What is the
        > best algorythm to do this?
        >
        > Right now I'm converting AUG to an 8 and then 10 to a day and assuming
        > that the year is always 2005.
        >
        > 8/10/2005 13:44:38
        >
        > There has to be a better way...?[/color]

        Well, the assumption of the year is a big problem that you will need to
        decide what the RIGHT thing to do is. If today were 1 Jan 2006 and you are
        handed the strings above, do you *want* it to assume 2006, or 2005, or
        what?

        A way to assume the current year, rather than 2005, is to substitute
        Year(Now) for 2005 in your existing code.

        Now ... a way to turn a string into a date value when you are positive you
        know the format, is the method Date.ParseExact . I ran this in the debugger
        (just paused at a breakpoint in one of my own programs and typed this in
        the Command Window):

        ? date.ParseExact ("10 AUG 13:44:38","dd MMM HH:mm:ss",nothi ng)
        #8/10/2005 1:44:38 PM#

        From what I can determine, the absent year in that string is going to be
        assumed to be the current year - again, that may or may not be what you
        want.

        Comment

        Working...