本文整理汇总了VB.NET中System.DateTime.ParseExact方法的典型用法代码示例。如果您正苦于以下问题:VB.NET DateTime.ParseExact方法的具体用法?VB.NET DateTime.ParseExact怎么用?VB.NET DateTime.ParseExact使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.DateTime 的用法示例。
在下文中一共展示了DateTime.ParseExact方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: Example
' 导入命名空间
Imports System.Globalization
Module Example
Public Sub Main()
Dim dateString, format As String
Dim result As Date
Dim provider As CultureInfo = CultureInfo.InvariantCulture
' Parse date-only value with invariant culture.
dateString = "06/15/2008"
format = "d"
Try
result = Date.ParseExact(dateString, format, provider)
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
Catch e As FormatException
Console.WriteLine("{0} is not in the correct format.", dateString)
End Try
' Parse date-only value without leading zero in month using "d" format.
' Should throw a FormatException because standard short date pattern of
' invariant culture requires two-digit month.
dateString = "6/15/2008"
Try
result = Date.ParseExact(dateString, format, provider)
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
Catch e As FormatException
Console.WriteLine("{0} is not in the correct format.", dateString)
End Try
' Parse date and time with custom specifier.
dateString = "Sun 15 Jun 2008 8:30 AM -06:00"
format = "ddd dd MMM yyyy h:mm tt zzz"
Try
result = Date.ParseExact(dateString, format, provider)
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
Catch e As FormatException
Console.WriteLine("{0} is not in the correct format.", dateString)
End Try
' Parse date and time with offset but without offset's minutes.
' Should throw a FormatException because "zzz" specifier requires leading
' zero in hours.
dateString = "Sun 15 Jun 2008 8:30 AM -06"
Try
result = Date.ParseExact(dateString, format, provider)
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
Catch e As FormatException
Console.WriteLine("{0} is not in the correct format.", dateString)
End Try
' Parse a date string using the French (France) culture.
dateString = "15/06/2008 08:30"
format = "g"
provider = New CultureInfo("fr-FR")
Try
result = Date.ParseExact(dateString, format, provider)
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
Catch e As FormatException
Console.WriteLine("{0} is not in the correct format.", dateString)
End Try
' Parse a date that includes seconds and milliseconds
' by using the French (France) and invariant cultures.
dateString = "18/08/2015 06:30:15.006542"
format = "dd/MM/yyyy HH:mm:ss.ffffff"
Try
result = Date.ParseExact(dateString, format, provider)
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
Catch e As FormatException
Console.WriteLine("{0} is not in the correct format.", dateString)
End Try
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:74,代码来源:DateTime.ParseExact 输出:
06/15/2008 converts to 6/15/2008 12:00:00 AM.
6/15/2008 is not in the correct format.
Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 7:30:00 AM.
Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.
15/06/2008 08:30 converts to 6/15/2008 8:30:00 AM.
18/08/2015 06:30:15.006542 converts to 8/18/2015 6:30:15 AM.
示例2: Example
' 导入命名空间
Imports System.Globalization
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"
Try
dateValue = Date.ParseExact(dateString, "g", enUS, DateTimeStyles.None)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
' Allow a leading space in the date string.
Try
dateValue = Date.ParseExact(dateString, "g", enUS, DateTimeStyles.AllowLeadingWhite)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
' Use custom formats with M and MM.
dateString = "5/01/2009 09:00"
Try
dateValue = Date.ParseExact(dateString, "M/dd/yyyy hh:mm", enUS, DateTimeStyles.None)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
' Allow a leading space in the date string.
Try
dateValue = Date.ParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, DateTimeStyles.None)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
' Parse a string with time zone information.
dateString = "05/01/2009 01:30:42 PM -05:00"
Try
dateValue = Date.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.None)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
' Allow a leading space in the date string.
Try
dateValue = Date.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, _
DateTimeStyles.AdjustToUniversal)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
' Parse a string representing UTC.
dateString = "2008-06-11T16:11:20.0904778Z"
Try
dateValue = Date.ParseExact(dateString, "o", CultureInfo.InvariantCulture, _
DateTimeStyles.None)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
Try
dateValue = Date.ParseExact(dateString, "o", CultureInfo.InvariantCulture, _
DateTimeStyles.RoundtripKind)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:84,代码来源:DateTime.ParseExact 输出:
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).
示例3: Example
' 导入命名空间
Imports System.Globalization
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",
"MM/d/yyyy HH:mm:ss.ffffff" }
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",
"08/28/2015 16:17:39.125", "08/28/2015 16:17:39.125000" }
Dim dateValue As DateTime
For Each dateString As String In dateStrings
Try
dateValue = DateTime.ParseExact(dateString, formats, _
New CultureInfo("en-US"), _
DateTimeStyles.None)
Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}' to a date.", dateString)
End Try
Next
End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:30,代码来源:DateTime.ParseExact 输出:
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.
Unable to convert '08/28/2015 16:17:39.125' to a date.
Converted '08/28/2015 16:17:39.125000' to 8/28/2015 4:17:39 PM.
注:本文中的System.DateTime.ParseExact方法示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论