當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET DateTimeOffset.TryParseExact方法代碼示例

本文整理匯總了VB.NET中System.DateTimeOffset.TryParseExact方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET DateTimeOffset.TryParseExact方法的具體用法?VB.NET DateTimeOffset.TryParseExact怎麽用?VB.NET DateTimeOffset.TryParseExact使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.DateTimeOffset的用法示例。


在下文中一共展示了DateTimeOffset.TryParseExact方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: formats

Dim conIn As TextReader = Console.In
 Dim conOut As TextWriter = Console.Out
 Dim tries As Integer = 0
 Dim input As String = String.Empty
 Dim formats() As String = {"M/dd/yyyy HH:m zzz", "MM/dd/yyyy HH:m zzz", _
                            "M/d/yyyy HH:m zzz", "MM/d/yyyy HH:m zzz", _
                            "M/dd/yy HH:m zzz", "MM/dd/yy HH:m zzz", _
                            "M/d/yy HH:m zzz", "MM/d/yy HH:m zzz", _                                 
                            "M/dd/yyyy H:m zzz", "MM/dd/yyyy H:m zzz", _
                            "M/d/yyyy H:m zzz", "MM/d/yyyy H:m zzz", _
                            "M/dd/yy H:m zzz", "MM/dd/yy H:m zzz", _
                            "M/d/yy H:m zzz", "MM/d/yy H:m zzz", _                               
                            "M/dd/yyyy HH:mm zzz", "MM/dd/yyyy HH:mm zzz", _
                            "M/d/yyyy HH:mm zzz", "MM/d/yyyy HH:mm zzz", _
                            "M/dd/yy HH:mm zzz", "MM/dd/yy HH:mm zzz", _
                            "M/d/yy HH:mm zzz", "MM/d/yy HH:mm zzz", _                                 
                            "M/dd/yyyy H:mm zzz", "MM/dd/yyyy H:mm zzz", _
                            "M/d/yyyy H:mm zzz", "MM/d/yyyy H:mm zzz", _
                            "M/dd/yy H:mm zzz", "MM/dd/yy H:mm zzz", _
                            "M/d/yy H:mm zzz", "MM/d/yy H:mm zzz"}   
 Dim provider As IFormatProvider = CultureInfo.InvariantCulture.DateTimeFormat
 Dim result As DateTimeOffset

 Do 
    conOut.WriteLine("Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),")
    conOut.Write("Then press Enter: ")
    input = conIn.ReadLine()
    conOut.WriteLine() 
    If DateTimeOffset.TryParseExact(input, formats, provider, _
                                    DateTimeStyles.AllowWhiteSpaces, _
                                    result) Then
       Exit Do
    Else
       Console.WriteLine("Unable to parse {0}.", input)      
       tries += 1
    End If
 Loop While tries < 3
 If tries >= 3 Then
    Console.WriteLine("Exiting application without parsing {0}", input)
 Else
    Console.WriteLine("{0} was converted to {1}", input, result.ToString())                                                     
 End If 
 ' Some successful sample interactions with the user might appear as follows:
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/08/2007 6:54 -6:00
 '    
 '    12/08/2007 6:54 -6:00 was converted to 12/8/2007 6:54:00 AM -06:00         
 '    
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/8/2007 06:54 -06:00
 '    
 '    12/8/2007 06:54 -06:00 was converted to 12/8/2007 6:54:00 AM -06:00
 '    
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/5/07 6:54 -6:00
 '    
 '    12/5/07 6:54 -6:00 was converted to 12/5/2007 6:54:00 AM -06:00
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:57,代碼來源:DateTimeOffset.TryParseExact

示例2:

Dim dateString, format As String  
Dim result As DateTimeOffset
Dim provider As CultureInfo = CultureInfo.InvariantCulture

' Parse date-only value with invariant culture and assume time is UTC.
dateString = "06/15/2008"
format = "d"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                   DateTimeStyles.AssumeUniversal, _
                                   result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If 

' Parse date-only value with leading white space.
' Should return False because only trailing white space is  
' specified in method call.
dateString = " 06/15/2008"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowTrailingWhite, _
                                result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If 

' Parse date and time value, and allow all white space.
dateString = " 06/15/   2008  15:15    -05:00"
format = "MM/dd/yyyy H:mm zzz"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowWhiteSpaces, _
                                result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If 

' Parse date and time and convert to UTC.
dateString = "  06/15/2008 15:15:30 -05:00"   
format = "MM/dd/yyyy H:mm:ss zzz"       
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowWhiteSpaces Or _
                                DateTimeStyles.AdjustToUniversal, _
                                result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:49,代碼來源:DateTimeOffset.TryParseExact

輸出:

06/15/2008' converts to 6/15/2008 12:00:00 AM +00:00.
06/15/2008' is not in the correct format.
06/15/   2008  15:15    -05:00' converts to 6/15/2008 3:15:00 PM -05:00.
06/15/2008 15:15:30 -05:00' converts to 6/15/2008 8:15:30 PM +00:00.

示例3: Example

' 導入命名空間
Imports System.Globalization

Public Module Example
   Public Sub Main()
      Dim dateStrings() = { "2018-08-18T12:45:16.0000000Z",
                            "2018/08/18T12:45:16.0000000Z",
                            "2018-18-08T12:45:16.0000000Z",
                            "2018-08-18T12:45:16.0000000",                               
                            " 2018-08-18T12:45:16.0000000Z ",
                            "2018-08-18T12:45:16.0000000+02:00",
                            "2018-08-18T12:45:16.0000000-07:00" } 
      
      ParseWithISO8601(dateStrings, DateTimeStyles.None)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AllowWhiteSpaces)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AdjustToUniversal)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeLocal)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeUniversal)   
   End Sub

   Private Sub ParseWithISO8601(dateStrings() As String, styles As DateTimeStyles)
      Console.WriteLine($"Parsing with {styles}:")
      Dim dat As DateTimeOffset
      For Each dateStr In dateStrings
         If DateTimeOffset.TryParseExact(dateStr, "O", Nothing, styles, dat) Then
            Console.WriteLine($"   {dateStr,-35} --> {dat:yyyy-MM-dd HH:mm:ss.FF zzz}")
         Else
            Console.WriteLine($"   Unable to convert '{dateStr}'")
         End If   
      Next 
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:36,代碼來源:DateTimeOffset.TryParseExact

輸出:

Parsing with None:
2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
Unable to convert '2018/08/18T12:45:16.0000000Z'
Unable to convert '2018-18-08T12:45:16.0000000Z'
2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
Unable to convert ' 2018-08-18T12:45:16.0000000Z '
2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00

-----

Parsing with AllowWhiteSpaces:
2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
Unable to convert '2018/08/18T12:45:16.0000000Z'
Unable to convert '2018-18-08T12:45:16.0000000Z'
2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
2018-08-18T12:45:16.0000000Z       --> 2018-08-18 12:45:16 +00:00
2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00

-----

Parsing with AdjustToUniversal:
2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
Unable to convert '2018/08/18T12:45:16.0000000Z'
Unable to convert '2018-18-08T12:45:16.0000000Z'
2018-08-18T12:45:16.0000000         --> 2018-08-18 19:45:16 +00:00
Unable to convert ' 2018-08-18T12:45:16.0000000Z '
2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 10:45:16 +00:00
2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 19:45:16 +00:00

-----

Parsing with AssumeLocal:
2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
Unable to convert '2018/08/18T12:45:16.0000000Z'
Unable to convert '2018-18-08T12:45:16.0000000Z'
2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
Unable to convert ' 2018-08-18T12:45:16.0000000Z '
2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00

-----

Parsing with AssumeUniversal:
2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
Unable to convert '2018/08/18T12:45:16.0000000Z'
Unable to convert '2018-18-08T12:45:16.0000000Z'
2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 +00:00
Unable to convert ' 2018-08-18T12:45:16.0000000Z '
2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00


注:本文中的System.DateTimeOffset.TryParseExact方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。