本文整理汇总了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