Translate a string to (that looks like) Date/Time format?

I have strings (as β€œ2022-04-22-131031”) that I have to transform into DateTime format (YYYY-MM-DD ss:ss:ss) and then separate Date and Time into two distinct strings (to display in a Listbox). How to do it? (the difficulty is that There is no separator (:slight_smile: between seconds for use DateTime.FromString(SQLDateTime) command for example).

Split your string by β€œ-”. The time needs to be split into 3 strings. Then use the constructor for date time with your array. See,_Month_as_Integer,_Day_as_Integer,hour_as_Integer%3D_0,minute_as_Integer%3D_0,second_as_Integer%3D_0,nanosecond_as_Integer%3D_0,timeZone_as_TimeZone%3D_Nil)

Beatrix basically outlined the solution before I could finish. But this code below explicitly shows the steps that I might do.

Var origFormat As String
origFormat = "2022-04-22-131031"
Var asPieces() As String
asPieces = origFormat.Split("-")
Var theYear, theMonth, theDay, theTime As String
theYear = asPieces(0)
theMonth = asPieces(1)
theDay = asPieces(2)
theTime = asPieces(3)
Var theHr, theMin, theSec As String
theHr = theTime.Left(2)
theMin = theTime.Middle(2, 2)
theSec = theTime.Left(2)
Var theSQL_DateTime As String
theSQL_DateTime = theYear + "-" + theMonth + "-" + theDay + " " + theHr + ":" + theMin + ":" + theSec
1 Like

There’s a typo in the code.

It should be:

theSec = theTime.Right(2)
1 Like

Thank you very much !