从文本字符串创建日期 - VB.Net

dc.*_*dc. 1 vb.net datetime

我在将字符串转换为我需要的日期格式时遇到问题.我试图转换的字符串的一个例子是215056081909.我想将该字符串转换为08/19/09 21:50:56.

下面列出的代码是我尝试用于转换过程的代码.当我运行代码时,我收到下面的错误,我很确定这是因为我的字符串在军事(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)))
Run Code Online (Sandbox Code Playgroud)

Cᴏʀ*_*ᴏʀʏ 7

由于您具有确切的格式,您可以使用DateTime.ParseExact提供格式和输入数据来尝试转换日期:

Dim theDate As DateTime = DateTime.ParseExact("215056081909", _
                                              "HHmmssMMddyy", _
                                              CultureInfo.CurrentCulture)
Run Code Online (Sandbox Code Playgroud)

您可能希望考虑使用DateTime.TryParseExact以防输入可能格式错误:

Dim theDate As DateTime
If DateTime.TryParseExact("215056081909", _
                          "HHmmssMMddyy", _
                          CultureInfo.CurrentCulture, _
                          DateTimeStyles.None, _
                          theDate)) Then
    ' Parsing succeeded, and theDate will contain the parsed date
End If
Run Code Online (Sandbox Code Playgroud)