本文整理匯總了VB.NET中System.DateTime.TryParseExact方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET DateTime.TryParseExact方法的具體用法?VB.NET DateTime.TryParseExact怎麽用?VB.NET DateTime.TryParseExact使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.DateTime
的用法示例。
在下文中一共展示了DateTime.TryParseExact方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
' 導入命名空間
Imports System.Globalization
Public Module Example
Public Sub Main()
Dim enUS As New CultureInfo("en-US")
Dim dateString As String
Dim dateValue As Date
' Parse date with no style flags.
dateString = " 5/01/2009 8:30 AM"
If Date.TryParseExact(dateString, "g", enUS, _
DateTimeStyles.None, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
' Allow a leading space in the date string.
If Date.TryParseExact(dateString, "g", enUS, _
DateTimeStyles.AllowLeadingWhite, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
' Use custom formats with M and MM.
dateString = "5/01/2009 09:00"
If Date.TryParseExact(dateString, "M/dd/yyyy hh:mm", enUS, _
DateTimeStyles.None, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
' Allow a leading space in the date string.
If Date.TryParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, _
DateTimeStyles.None, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
' Parse a string with time zone information.
dateString = "05/01/2009 01:30:42 PM -05:00"
If Date.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, _
DateTimeStyles.None, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
' Allow a leading space in the date string.
If Date.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, _
DateTimeStyles.AdjustToUniversal, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
' Parse a string representing UTC.
dateString = "2008-06-11T16:11:20.0904778Z"
If Date.TryParseExact(dateString, "o", CultureInfo.InvariantCulture, _
DateTimeStyles.None, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
If Date.TryParseExact(dateString, "o", CultureInfo.InvariantCulture, _
DateTimeStyles.RoundtripKind, dateValue) Then
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Else
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End If
End Sub
End Module
輸出:
5/01/2009 8:30 AM' is not in an acceptable format. Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified). Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified). 5/01/2009 09:00' is not in an acceptable format. Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local). Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc). Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local). Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).
示例2: Example
' 導入命名空間
Imports System.Globalization
Public Module Example
Public Sub Main()
Dim formats() As String = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", _
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", _
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", _
"M/d/yyyy h:mm", "M/d/yyyy h:mm", _
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm"}
Dim dateStrings() As String = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM", _
"5/1/2009 6:32:00", "05/01/2009 06:32", _
"05/01/2009 06:32:00 PM", "05/01/2009 06:32:00"}
Dim dateValue As DateTime
For Each dateString As String In dateStrings
If Date.TryParseExact(dateString, formats, _
New CultureInfo("en-US"), _
DateTimeStyles.None, _
dateValue) Then
Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue)
Else
Console.WriteLine("Unable to convert '{0}' to a date.", dateString)
End If
Next
End Sub
End Module
輸出:
Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM. Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM. Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM. Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM. Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM. Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.