這裏有一個簡單的函數,將採取格式化喜歡你的任何輸入您指定格式的字符串,並返回格式化像你輸出字符串:
Private Function FormatTime(Input As String, Format as String) As String
Dim TimeFormat As New DateTime
Dim GoodString As Boolean = DateTime.TryParse(Input, TimeFormat)
If Not GoodString Then
If Integer.TryParse(Input, vbNull) Then
If Input.Length > 2 Then
Input = Input.PadLeft(4, "0"c)
TimeFormat = New DateTime(Now.Year, Now.Month, Now.Day, Integer.Parse(Input.Substring(0, 2)), Integer.Parse(Input.Substring(2)), 0)
Else
TimeFormat = New DateTime(Now.Year, Now.Month, Now.Day, Integer.Parse(Input.PadLeft(2, "0"c).Substring(0, 2)), 0, 0)
End If
End If
End If
Return TimeFormat.ToString(Format)
End Function
這並不簡單驗證爲好。它檢查正確的時間格式,如果不是,如果所有字符是數字
你的說法是這樣的:
`TimeFormatVariable = FormatTime(OriginalTimeInput,「HHMM」)
您的輸入看起來像壞串。你怎麼知道'01'是一小時而不是一分鐘? – LarsTech
[有沒有一種方法可以將VB6格式化字符串轉換爲.NET格式化字符串?](http://stackoverflow.com/questions/4072490/is-there-a-way-to-programmatically-convert-vb6 -formatting-strings-to-net-format) –
在VB6格式中不起作用。例如「0120」產生「0000」 – tinstaafl