本文整理匯總了VB.NET中System.TimeSpan結構體的典型用法代碼示例。如果您正苦於以下問題:VB.NET TimeSpan結構體的具體用法?VB.NET TimeSpan怎麽用?VB.NET TimeSpan使用的例子?那麽, 這裏精選的結構體代碼示例或許可以為您提供幫助。
在下文中一共展示了TimeSpan結構體的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1:
' Define two dates.
Dim date1 As Date = #1/1/2010 8:00:15AM#
Dim date2 As Date = #8/18/2010 1:30:30PM#
' Calculate the interval between the two dates.
Dim interval As TimeSpan = date2 - date1
Console.WriteLine("{0} - {1} = {2}", date2, date1, interval.ToString())
' Display individual properties of the resulting TimeSpan object.
Console.WriteLine(" {0,-35} {1,20}", "Value of Days Component:", interval.Days)
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays)
Console.WriteLine(" {0,-35} {1,20}", "Value of Hours Component:", interval.Hours)
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours)
Console.WriteLine(" {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes)
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes)
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Ticks:", interval.Ticks)
輸出:
8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15 Value of Days Component: 229 Total Number of Days: 229.229340277778 Value of Hours Component: 5 Total Number of Hours: 5501.50416666667 Value of Minutes Component: 30 Total Number of Minutes: 330090.25 Value of Seconds Component: 15 Total Number of Seconds: 19,805,415 Value of Milliseconds Component: 0 Total Number of Milliseconds: 19,805,415,000 Ticks: 198,054,150,000,000
示例2:
Dim departure As DateTime = #06/12/2010 6:32PM#
Dim arrival As DateTime = #06/13/2010 10:47PM#
Dim travelTime As TimeSpan = arrival - departure
Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime)
輸出:
6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00
示例3: Example
Module Example
Dim rnd As New Random()
Public Sub Main()
Dim timeSpent As TimeSpan = TimeSpan.Zero
timeSpent += GetTimeBeforeLunch()
timeSpent += GetTimeAfterLunch()
Console.WriteLine("Total time: {0}", timeSpent)
End Sub
Private Function GetTimeBeforeLunch() As TimeSpan
Return New TimeSpan(rnd.Next(3, 6), 0, 0)
End Function
Private Function GetTimeAfterLunch() As TimeSpan
Return New TimeSpan(rnd.Next(3, 6), 0, 0)
End Function
End Module
輸出:
Total time: 08:00:00
示例4: values
Dim values() As String = { "12", "31.", "5.8:32:16", "12:12:15.95", ".12"}
For Each value As String In values
Try
Dim ts As TimeSpan = TimeSpan.Parse(value)
Console.WriteLine("'{0}' --> {1}", value, ts)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'", value)
Catch e As OverflowException
Console.WriteLine("'{0}' is outside the range of a TimeSpan.", value)
End Try
Next
輸出:
12' --> 12.00:00:00 Unable to parse '31.' 5.8:32:16' --> 5.08:32:16 12:12:15.95' --> 12:12:15.9500000 Unable to parse '.12'
示例5: TimeSpan
Dim interval As New TimeSpan(12, 30, 45)
Dim output As String
Try
output = String.Format("{0:r}", interval)
Catch e As FormatException
output = "Invalid Format"
End Try
Console.WriteLine(output)
' Output from .NET Framework 3.5 and earlier versions:
' 12:30:45
' Output from .NET Framework 4:
' Invalid Format
示例6: Example
Module Example
Public Sub Main()
Dim appSetup As New AppDomainSetup()
appSetup.SetCompatibilitySwitches( { "NetFx40_TimeSpanLegacyFormatMode" } )
Dim legacyDomain As AppDomain = AppDomain.CreateDomain("legacyDomain",
Nothing, appSetup)
legacyDomain.ExecuteAssembly("ShowTimeSpan.exe")
End Sub
End Module
示例7: Example
Module Example
Public Sub Main()
Dim interval As TimeSpan = Date.Now - Date.Now.Date
Dim msg As String = String.Format("Elapsed Time Today: {0:d} hours.",
interval)
Console.WriteLine(msg)
End Sub
End Module
輸出:
Elapsed Time Today: 01:40:52.2524662 hours.
示例8: New TimeSpan
' 導入命名空間
Imports System
Public Class MainClass
Shared Sub Main(ByVal args As String())
Dim ts1, ts2 As TimeSpan
ts1 = New TimeSpan(1, 2, 3)
ts2 = New TimeSpan(ts1.Ticks * 12)
Console.WriteLine(ts1.ToString)
Console.WriteLine(ts2.ToString)
End Sub
End Class