本文整理汇总了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.