I having a problem converting a string into the date format I need. An example of the string I'm trying to convert would be 215056081909. I would like to convert that string into 08/19/09 21:50:56.
The code listed below is what I'm trying to use for the conversion process. When I run the code I'm getting the error below and I'm pretty sure it's because my string has the time portion in military (24 hour) time. Could anyone help me get the string converted to the format I need?
Thank you!
Error:
System.ArgumentOutOfRangeException was unhandled
Message="Index and length must refer to a location within the string. Parameter name: length"
ParamName="length"
Source="mscorlib"
StackTrace:
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at GLTracker.PortManager.DoDataCollection(MessageType type, String msg)
at GLTracker.PortManager.comPort_DataReceived(Object sender, SerialDataReceivedEventArgs e)
at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src, SerialDataReceivedEventArgs e)
at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object state)
at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)
InnerException:
'Code:
Dim strDateTime as string = "215056081909"
Dim dTransDate As DateTime = Convert.ToDateTime(String.Format("{0}/{1}/{2} {3}:{4}:{5}", _
strDateTime.Substring(7, 2), _
strDateTime.Substring(9, 2), _
strDateTime.Substring(11, 2), _
strDateTime.Substring(0, 2), _
strDateTime.Substring(3, 2), _
strDateTime.Substring(5, 2)))