我有问题将字符串转换为我需要的日期格式。我试图转换的字符串的一个例子是215056081909.我想将该字符串转换为08/19/09 21:50:56。从文本字符串创建日期 - VB.Net
下面列出的代码是我试图用于转换过程。当我运行代码时,我得到下面的错误,我很确定这是因为我的字符串在军事(24小时)时间中有时间部分。任何人都可以帮我把字符串转换成我需要的格式吗?
谢谢!
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)))
这工作很棒...谢谢。 – 2009-09-20 06:05:23