作者:VB.NET开发
项目:Syste
' 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)
作者:VB.NET开发
项目:Syste
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)
作者:VB.NET开发
项目:Syste
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
作者:VB.NET开发
项目:Syste
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
作者:VB.NET开发
项目:Syste
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
作者:VB.NET开发
项目:Syste
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
作者:VB.NET开发
项目:Syste
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
作者:VB程序
项目:Syste
' 导入命名空间
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