当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET DateTime.Parse方法代码示例

本文整理汇总了VB.NET中System.DateTime.Parse方法的典型用法代码示例。如果您正苦于以下问题:VB.NET DateTime.Parse方法的具体用法?VB.NET DateTime.Parse怎么用?VB.NET DateTime.Parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.DateTime的用法示例。


在下文中一共展示了DateTime.Parse方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: Strings

Public Module Strings
   Public Sub Main()
      Dim dateInfo() As (dateAsString As String, description As String) = 
                     { ("08/18/2018 07:22:16", "String with a date and time component"),
                       ("08/18/2018", "String with a date component only"),
                       ("8/2018", "String with a month and year component only"),
                       ("8/18", "String with a month and day component only"),
                       ("07:22:16", "String with a time component only"),
                       ("7 PM", "String with an hour and AM/PM designator only"),
                       ("2018-08-18T07:22:16.0000000Z", "UTC string that conforms to ISO 8601"),   
                       ("2018-08-18T07:22:16.0000000-07:00", "Non-UTC string that conforms to ISO 8601"),
                       ("Sat, 18 Aug 2018 07:22:16 GMT", "String that conforms to RFC 1123"),
                       ("08/18/2018 07:22:16 -5:00", "String with date, time, and time zone information" ) }
   
      Console.WriteLine($"Today is {Date.Now:d}{vbCrLf}")
      
      For Each item in dateInfo
         Console.WriteLine($"{item.description + ":",-52} '{item.dateAsString}' --> {DateTime.Parse(item.dateAsString)}")        
      Next
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:21,代码来源:DateTime.Parse

输出:

Today is 2/22/2018

String with a date and time component:               '08/18/2018 07:22:16' --> 8/18/2018 7:22:16 AM
String with a date component only:                   '08/18/2018' --> 8/18/2018 12:00:00 AM
String with a month and year component only:         '8/2018' --> 8/1/2018 12:00:00 AM
String with a month and day component only:          '8/18' --> 8/18/2018 12:00:00 AM
String with a time component only:                   '07:22:16' --> 2/22/2018 7:22:16 AM
String with an hour and AM/PM designator only:       '7 PM' --> 2/22/2018 7:00:00 PM
UTC string that conforms to ISO 8601:                '2018-08-18T07:22:16.0000000Z' --> 8/18/2018 12:22:16 AM
Non-UTC string that conforms to ISO 8601:            '2018-08-18T07:22:16.0000000-07:00' --> 8/18/2018 7:22:16 AM
String that conforms to RFC 1123:                    'Sat, 18 Aug 2018 07:22:16 GMT' --> 8/18/2018 12:22:16 AM
String with date, time, and time zone information:   '08/18/2018 07:22:16 -5:00' --> 8/18/2018 5:22:16 AM

示例2: Example

Module Example
   Public Sub Main()
      Dim dateStrings() As String = {"2008-05-01T07:34:42-5:00", 
                                     "2008-05-01 7:34:42Z", 
                                     "Thu, 01 May 2008 07:34:42 GMT"}
      
      For Each dateStr In dateStrings
         Dim convertedDate As Date = Date.Parse(dateStr)
         Console.WriteLine($"Converted {dateStr} to {convertedDate.Kind} time {convertedDate}")
      Next 
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:12,代码来源:DateTime.Parse

输出:

Converted 2008-05-01T07:34:42-5:00 to Local time 5/1/2008 5:34:42 AM
Converted 2008-05-01 7:34:42Z to Local time 5/1/2008 12:34:42 AM
Converted Thu, 01 May 2008 07:34:42 GMT to Local time 5/1/2008 12:34:42 AM

示例3: formattedDates

Dim formattedDates() = { "2008-09-15T09:30:41.7752486-07:00", 
                           "2008-09-15T09:30:41.7752486Z",  
                           "2008-09-15T09:30:41.7752486",  
                           "2008-09-15T09:30:41.7752486-04:00", 
                           "Mon, 15 Sep 2008 09:30:41 GMT" }
For Each formattedDate In formattedDates
   Console.WriteLine(formattedDate)
   Dim roundtripDate = DateTime.Parse(formattedDate, Nothing,  
                                      DateTimeStyles.RoundtripKind)                        
   Console.WriteLine($"   With RoundtripKind flag: {roundtripDate} {roundtripDate.Kind} time.")                                          
   Dim noRoundtripDate = DateTime.Parse(formattedDate, Nothing,                                                                                                  DateTimeStyles.None)
   Console.WriteLine($"   Without RoundtripKind flag: {noRoundtripDate} {noRoundtripDate.Kind} time.")
Next
开发者ID:VB.NET开发者,项目名称:System,代码行数:13,代码来源:DateTime.Parse

输出:

2008-09-15T09:30:41.7752486-07:00
With RoundtripKind flag: 9/15/2008 9:30:41 AM Local time.
Without RoundtripKind flag: 9/15/2008 9:30:41 AM Local time.
2008-09-15T09:30:41.7752486Z
With RoundtripKind flag: 9/15/2008 9:30:41 AM Utc time.
Without RoundtripKind flag: 9/15/2008 2:30:41 AM Local time.
2008-09-15T09:30:41.7752486
With RoundtripKind flag: 9/15/2008 9:30:41 AM Unspecified time.
Without RoundtripKind flag: 9/15/2008 9:30:41 AM Unspecified time.
2008-09-15T09:30:41.7752486-04:00
With RoundtripKind flag: 9/15/2008 6:30:41 AM Local time.
Without RoundtripKind flag: 9/15/2008 6:30:41 AM Local time.
Mon, 15 Sep 2008 09:30:41 GMT
With RoundtripKind flag: 9/15/2008 9:30:41 AM Utc time.
Without RoundtripKind flag: 9/15/2008 2:30:41 AM Local time.

示例4: DateTimeParser

' 导入命名空间
Imports System.Globalization

Class DateTimeParser
   Public Shared Sub Main()
      ' Assume the current culture is en-US. 
      ' The date is February 16, 2008, 12 hours, 15 minutes and 12 seconds.

      ' Use standard en-US date and time value
      Dim dateValue As Date
      Dim dateString As String = "2/16/2008 12:15:12 PM"
      Try
         dateValue = Date.Parse(dateString)
         Console.WriteLine("'{0}' converted to {1}.", dateString, dateValue)
      Catch e As FormatException
         Console.WriteLine("Unable to convert '{0}'.", dateString)
      End Try
            
      ' Reverse month and day to conform to the fr-FR culture.
      ' The date is February 16, 2008, 12 hours, 15 minutes and 12 seconds.
      dateString = "16/02/2008 12:15:12"
      Try
         dateValue = Date.Parse(dateString)
         Console.WriteLine("'{0}' converted to {1}.", dateString, dateValue)
      Catch e As FormatException
         Console.WriteLine("Unable to convert '{0}'.", dateString)
      End Try

      ' Call another overload of Parse to successfully convert string
      ' formatted according to conventions of fr-FR culture.      
      Try
         dateValue = Date.Parse(dateString, New CultureInfo("fr-FR", False))
         Console.WriteLine("'{0}' converted to {1}.", dateString, dateValue)
      Catch e As FormatException
         Console.WriteLine("Unable to convert '{0}'.", dateString)
      End Try
      
      ' Parse string with date but no time component.
      dateString = "2/16/2008"
      Try
         dateValue = Date.Parse(dateString)
         Console.WriteLine("'{0}' converted to {1}.", dateString, dateValue)
      Catch e As FormatException
         Console.WriteLine("Unable to convert '{0}'.", dateString)
      End Try
   End Sub 
End Class
开发者ID:VB.NET开发者,项目名称:System,代码行数:47,代码来源:DateTime.Parse

输出:

2/16/2008 12:15:12 PM' converted to 2/16/2008 12:15:12 PM.
Unable to convert '16/02/2008 12:15:12'.
16/02/2008 12:15:12' converted to 2/16/2008 12:15:12 PM.
2/16/2008' converted to 2/16/2008 12:00:00 AM.

示例5: ParseDate

' 导入命名空间
Imports System.Globalization

Module ParseDate
   Public Sub Main()
      ' Define cultures to be used to parse dates.
      Dim cultures() As CultureInfo = {CultureInfo.CreateSpecificCulture("en-US"), _
                                       CultureInfo.CreateSpecificCulture("fr-FR"), _
                                       CultureInfo.CreateSpecificCulture("de-DE")}
      ' Define string representations of a date to be parsed.
      Dim dateStrings() As String = {"01/10/2009 7:34 PM", _
                                     "10.01.2009 19:34", _
                                     "10-1-2009 19:34" }
      ' Parse dates using each culture.
      For Each culture In cultures
         Dim dateValue As Date
         Console.WriteLine("Attempted conversions using {0} culture.", culture.Name)
         For Each dateString As String In dateStrings
            Try
               dateValue = Date.Parse(dateString, culture)
               Console.WriteLine("   Converted '{0}' to {1}.", _
                                 dateString, dateValue.ToString("f", culture))
            Catch e As FormatException
               Console.WriteLine("   Unable to convert '{0}' for culture {1}.", _
                                 dateString, culture.Name)
            End Try                                                
         Next
         Console.WriteLine()
      Next                                                                                     
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:31,代码来源:DateTime.Parse

输出:

Attempted conversions using en-US culture.
Converted '01/10/2009 7:34 PM' to Saturday, January 10, 2009 7:34 PM.
Converted '10.01.2009 19:34' to Thursday, October 01, 2009 7:34 PM.
Converted '10-1-2009 19:34' to Thursday, October 01, 2009 7:34 PM.

Attempted conversions using fr-FR culture.
Converted '01/10/2009 7:34 PM' to jeudi 1 octobre 2009 19:34.
Converted '10.01.2009 19:34' to samedi 10 janvier 2009 19:34.
Converted '10-1-2009 19:34' to samedi 10 janvier 2009 19:34.

Attempted conversions using de-DE culture.
Converted '01/10/2009 7:34 PM' to Donnerstag, 1. Oktober 2009 19:34.
Converted '10.01.2009 19:34' to Samstag, 10. Januar 2009 19:34.
Converted '10-1-2009 19:34' to Samstag, 10. Januar 2009 19:34.

示例6: ParseDateExample

' 导入命名空间
Imports System.Globalization

Module ParseDateExample
   Public Sub Main()
      Dim dateString As String  
      Dim culture As CultureInfo
      Dim styles As DateTimeStyles 
      Dim result As DateTime
      
      ' Parse a date and time with no styles.
      dateString = "03/01/2009 10:00 AM"
      culture = CultureInfo.CreateSpecificCulture("en-US")
      styles = DateTimeStyles.None
      Try
         result = DateTime.Parse(dateString, culture, styles)
         Console.WriteLine("{0} converted to {1} {2}.", _
                           dateString, result, result.Kind.ToString())
      Catch e As FormatException
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
      End Try      
      
      ' Parse the same date and time with the AssumeLocal style.
      styles = DateTimeStyles.AssumeLocal
      Try
         result = DateTime.Parse(dateString, culture, styles)
         Console.WriteLine("{0} converted to {1} {2}.", _
                           dateString, result, result.Kind.ToString())
      Catch e As FormatException
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
      End Try      
      
      ' Parse a date and time that is assumed to be local.
      ' This time is five hours behind UTC. The local system's time zone is 
      ' eight hours behind UTC.
      dateString = "2009/03/01T10:00:00-5:00"
      styles = DateTimeStyles.AssumeLocal
      Try
         result = DateTime.Parse(dateString, culture, styles)
         Console.WriteLine("{0} converted to {1} {2}.", _
                           dateString, result, result.Kind.ToString())
      Catch e As FormatException
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
      End Try      
      
      ' Attempt to convert a string in improper ISO 8601 format.
      dateString = "03/01/2009T10:00:00-5:00"
      Try
         result = DateTime.Parse(dateString, culture, styles)
         Console.WriteLine("{0} converted to {1} {2}.", _
                           dateString, result, result.Kind.ToString())
      Catch e As FormatException
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
      End Try      

      ' Assume a date and time string formatted for the fr-FR culture is the local 
      ' time and convert it to UTC.
      dateString = "2008-03-01 10:00"
      culture = CultureInfo.CreateSpecificCulture("fr-FR")
      styles = DateTimeStyles.AdjustToUniversal Or DateTimeStyles.AssumeLocal
      Try
         result = DateTime.Parse(dateString, culture, styles)
         Console.WriteLine("{0} converted to {1} {2}.", _
                           dateString, result, result.Kind.ToString())
      Catch e As FormatException
         Console.WriteLine("Unable to convert {0} to a date and time.", dateString)
      End Try      
   End Sub
End Module
开发者ID:VB.NET开发者,项目名称:System,代码行数:69,代码来源:DateTime.Parse

输出:

03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Unspecified.
03/01/2009 10:00 AM converted to 3/1/2009 10:00:00 AM Local.
2009/03/01T10:00:00-5:00 converted to 3/1/2009 7:00:00 AM Local.
Unable to convert 03/01/2009T10:00:00-5:00 to a date and time.
2008-03-01 10:00 converted to 3/1/2008 6:00:00 PM Utc.


注:本文中的System.DateTime.Parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。