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


VB.NET TimeSpan.TryParse方法代碼示例

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


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

示例1: TryParse

Module TryParse
    Sub ParseTimeSpan( intervalStr As String )
        ' Write the first part of the output line.
        Console.Write( "{0,20}   ", intervalStr )

        ' Parse the parameter, and then convert it back to a string.
         Dim intervalVal As TimeSpan 
         If TimeSpan.TryParse( intervalStr, intervalVal ) Then
            Dim intervalToStr As String = intervalVal.ToString( )
   
            ' Pad the end of the TimeSpan string with spaces if it 
            ' does not contain milliseconds.
            Dim pIndex As Integer = intervalToStr.IndexOf( ":"c )
            pIndex = intervalToStr.IndexOf( "."c, pIndex )
            If pIndex < 0 Then   intervalToStr &= "        "
   
            Console.WriteLine( "{0,21}", intervalToStr )
         ' Handle failure of TryParse method.
         Else
            Console.WriteLine("Parse operation failed.")
        End If
    End Sub 

    Public Sub Main( )
        Console.WriteLine( "{0,20}   {1,21}", _
            "String to Parse", "TimeSpan" )    
        Console.WriteLine( "{0,20}   {1,21}", _
            "---------------", "---------------------" )    

        ParseTimeSpan("0")
        ParseTimeSpan("14")
        ParseTimeSpan("1:2:3")
        ParseTimeSpan("0:0:0.250")
        ParseTimeSpan("10.20:30:40.50")
        ParseTimeSpan("99.23:59:59.9999999")
        ParseTimeSpan("0023:0059:0059.0099")
        ParseTimeSpan("23:0:0")
        ParseTimeSpan("24:0:0")
        ParseTimespan("0:59:0")
        ParseTimeSpan("0:60:0")
        ParseTimespan("0:0:59")
        ParseTimeSpan("0:0:60")
        ParseTimeSpan("10:")
        ParsetimeSpan("10:0")
        ParseTimeSpan(":10")
        ParseTimeSpan("0:10")
        ParseTimeSpan("10:20:")
        ParseTimeSpan("10:20:0")
        ParseTimeSpan(".123")
        ParseTimeSpan("0.12:00")
        ParseTimeSpan("10.")
        ParseTimeSpan("10.12")
        ParseTimeSpan("10.12:00")
    End Sub 
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:55,代碼來源:TimeSpan.TryParse

輸出:

String to Parse                TimeSpan
---------------   ---------------------
0        00:00:00
14     14.00:00:00
1:2:3        01:02:03
0:0:0.250        00:00:00.2500000
10.20:30:40.50     10.20:30:40.5000000
99.23:59:59.9999999     99.23:59:59.9999999
0023:0059:0059.0099        23:59:59.0099000
23:0:0        23:00:00
24:0:0   Parse operation failed.
0:59:0        00:59:00
0:60:0   Parse operation failed.
0:0:59        00:00:59
0:0:60   Parse operation failed.
10:   Parse operation failed.
10:0        10:00:00
:10   Parse operation failed.
0:10        00:10:00
10:20:   Parse operation failed.
10:20:0        10:20:00
.123   Parse operation failed.
0.12:00        12:00:00
10.   Parse operation failed.
10.12   Parse operation failed.
10.12:00     10.12:00:00

示例2:

Dim value As String = "000000006"
Dim interval As TimeSpan
If TimeSpan.TryParse(value, interval) Then
   Console.WriteLine("{0} --> {1}", value, interval)
Else
   Console.WriteLine("Unable to parse '{0}'", value)
End If
' Output from .NET Framework 3.5 and earlier versions:
'       000000006 --> 6.00:00:00
' Output from .NET Framework 4:
'       Unable to parse '000000006'
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:11,代碼來源:TimeSpan.TryParse

示例3: Example

' 導入命名空間
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim values() As String = { "6", "6:12", "6:12:14", "6:12:14:45", 
                                 "6.12:14:45", "6:12:14:45.3448", 
                                 "6:12:14:45,3448", "6:34:14:45" }
      Dim cultures() As CultureInfo = { New CultureInfo("en-US"), 
                                        New CultureInfo("ru-RU"),
                                        CultureInfo.InvariantCulture }
      
      Dim header As String = String.Format("{0,-17}", "String")
      For Each culture As CultureInfo In cultures
         header += If(culture.Equals(CultureInfo.InvariantCulture), 
                      String.Format("{0,20}", "Invariant"),
                      String.Format("{0,20}", culture.Name))
      Next
      Console.WriteLine(header)
      Console.WriteLine()
      
      For Each value As String In values
         Console.Write("{0,-17}", value)
         For Each culture As CultureInfo In cultures
            Dim interval As New TimeSpan()
            If TimeSpan.TryParse(value, culture, interval) Then
               Console.Write("{0,20}", interval.ToString("c"))
            Else
               Console.Write("{0,20}", "Unable to Parse")
            End If     
         Next
         Console.WriteLine()                                
      Next
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:35,代碼來源:TimeSpan.TryParse

輸出:

String                          en-US               ru-RU           Invariant

6                          6.00:00:00          6.00:00:00          6.00:00:00
6:12                         06:12:00            06:12:00            06:12:00
6:12:14                      06:12:14            06:12:14            06:12:14
6:12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
6.12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
6:12:14:45.3448    6.12:14:45.3448000     Unable to Parse  6.12:14:45.3448000
6:12:14:45,3448       Unable to Parse  6.12:14:45.3448000     Unable to Parse
6:34:14:45            Unable to Parse     Unable to Parse     Unable to Parse


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