Date assembly

P

pmclinn

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...?
 
H

Herfried K. Wagner [MVP]

pmclinn said:
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.

\\\
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 _
) _
)
///
 




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(dateSegments.Length)

dateSegments(dateSegments.GetUpperBound(0)) = Now.ToString("yyyy")

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

Console.Write(derivedDate.ToString("MM/dd/yyyy HH:mm:ss"))
 
R

Ross Presser

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...?

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",nothing)
#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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top