You often need to represent intervals of time in your applications, especially in conjunction with dates. The .NET Framework provides a structure for this: a value type named System.TimeSpan
. This structure can represent time from a minimum value (one tick) until a maximum value (one day). A tick is the smallest unit for time representations and is equal to 100 nanoseconds. TimeSpan
represents a summed amount of time between two given time values, and the time portion of a Date
object represents a single specific moment in time.
Minimum and Maximum Values
As for other value types, System.TimeSpan
also provides two shared properties named MinValue
and MaxValue
. MinValue
returns -10675199.02:48:05.4775808
, and MaxValue
returns 10675199.02:48:05.4775807
. For the sake of clarity, these values are equal to, respectively, System.Int64.MinValue
and System.Int64.MaxValue
.
You can find other cases in which TimeSpan
is needed for something other than simply working with dates. For example, you might want to create performance benchmarks by using the StopWatch
object, which returns a TimeSpan
. Or you might need such structure when working with animations in WPF applications. The following code example simulates a performance test; a System.StopWatch
object is started, an intensive loop is performed, and then the StopWatch
is stopped. The StopWatch
class offers an Elapsed
property that is of type TimeSpan
and can be useful for analyzing the amount of elapsed time:
Dim watch As New Stopwatch
watch.Start()
For i = 0 To 10000
'Simulates intensive processing
System.Threading.Thread.SpinWait(800000)
Next
watch.Stop()
Console.WriteLine(watch.Elapsed.Seconds)
Console.WriteLine(watch.Elapsed.Milliseconds)
Console.WriteLine(watch.Elapsed.Ticks)
The preceding code produced the following result on my machine, but it will be different on yours, depending on your hardware:
35
480
354800566
The TimeSpan
structure is similar to the area of the DateTime
type that is related to time. Notice that TimeSpan
offers several similar properties, such as Days
; Hours
, Minutes
, Seconds
, and Milliseconds
, and methods, such as AddDays
, AddHours
, AddMinute
, and Subtract
. TimeSpan
is all about time; this means that although there are similarities, as mentioned before, with the time-related DateTime
members, you cannot (obviously) work with dates. The following code provides an example of creating a TimeSpan
instance starting from an existing date:
Sub TimeSpanInstance()
Dim currentDate As Date = Date.Now
'Because the System namespace is imported at project
'level, we do not need an Imports directive
Dim intervalOfTime As TimeSpan = currentDate.TimeOfDay
Console.WriteLine("My friend, in the current date " &
"there are {0} days; time is {1}:{2}:{3}",
intervalOfTime.Days,
intervalOfTime.Hours,
intervalOfTime.Minutes,
intervalOfTime.Seconds)
End Sub
The preceding code produces the following result:
My friend, in the current date there are 0 days; time is 8:30
In the specified interval, there is only the current day, so the first argument returns zero. Take a look back at the section “Subtracting Dates and Adding Time to Time” to see an example of using TimeSpan
for an interval of time retrieved by subtracting two dates.
3.16.66.156