(1) TimeSpan subtract TimeSpan return TimeSpan
Private Sub SubtractTimeSpanFromTimeSpan()
Dim sdx As String
Dim ts2 As TimeSpan
Dim ts0 = New TimeSpan(21, 40, 0) ' 21 hours 40 minutes
Dim ts1 = New TimeSpan(8, 15, 30) ' 8 hours 15 minutes 30 seconds
ts2 = ts0.Subtract(ts1) ' using .Subtract function
sdx = String.Format("{0} minus {1} = {2}", ts0, ts1, ts2)
Console.WriteLine(sdx)
ts2 = ts0 - ts1 ' using - operator
sdx = String.Format("{0,-10} - {1,10} = {2}", ts0, ts1, ts2)
Console.WriteLine(sdx)
End Sub
Output:
21:40:00 minus 08:15:30 = 13:24:30
21:40:00 - 08:15:30 = 13:24:30
Two ways of subtracting two TimeSpans. Using the .Subtract function
and the - operator.
(2) TimeSpan constructors
Private Sub TimeSpanConstructors()
Dim days = 10
Dim hours = 20
Dim minutes = 30
Dim seconds = 45
Dim milliseconds = 500
Dim tiks As Long = 64000000000000
Dim ts1 = New TimeSpan(hours, minutes, seconds)
Dim ts2 = New TimeSpan(days, hours, minutes, seconds)
Dim ts3 = New TimeSpan(days, hours, minutes, seconds, milliseconds)
Dim ts4 = New TimeSpan(tiks)
Console.WriteLine(ts1.ToString)
Console.WriteLine(ts2.ToString)
Console.WriteLine(ts3.ToString)
Console.WriteLine(ts4.ToString)
End Sub
Output:
20:30:45
10.20:30:45
10.20:30:45.5000000
74.01:46:40
It depends on how much detail you need in a TimeSpan when you New up
one. If you don't need whole days you can use the hour, minute, second
constructor. If milliseconds is important that is available also. The one
parameter timespan takes a long. I arbitrarily chose Dim tiks As Long = 64000000000000
and I didn't have an idea of how much time that represented. As it turned out, over
74 days.
(3) TimeSpan compare functions
Private Sub CompareTimes()
Dim sdx As String
Dim answer As Integer
Dim ts0 = New TimeSpan(3, 4, 5, 0, 0)
Dim ts1 = New TimeSpan(3, 4, 5, 6, 0)
sdx = String.Format("ts0 = {0} ts1 = {1}", ts0, ts1)
Console.WriteLine(sdx)
answer = TimeSpan.Compare(ts0, ts1) ' equivalent to ts0.CompareTo(ts1)
sdx = String.Format("TimeSpan.Compare returned = {0}", answer)
Console.WriteLine(sdx)
Select Case answer
Case Is <= -1
Console.WriteLine("ts0 is less than ts1")
Case Is = 0
Console.WriteLine("ts0 is equal to ts1")
Case Is = 1
Console.WriteLine("ts0 is more than ts1")
Case Else
Console.WriteLine("something is all messed up")
End Select
answer = ts0.CompareTo(ts1)
sdx = String.Format("TimeSpan.CompareTo returned = {0}", answer)
Console.WriteLine(sdx)
Select Case answer
Case Is <= -1
Console.WriteLine("ts0 is less than ts1")
Case Is = 0
Console.WriteLine("ts0 is equal to ts1")
Case Is = 1
Console.WriteLine("ts0 is more than ts1")
Case Else
Console.WriteLine("something is all messed up")
End Select
End Sub
Output:
ts0 = 3.04:05:00 ts1 = 3.04:05:06
TimeSpan.Compare returned = -1
ts0 is less than ts1
TimeSpan.CompareTo returned = -1
ts0 is less than ts1
Either TimeSpan.Compare and TimeSpan.CompareTo return the same
thing. The only difference is the way they are called. CompareTo
requires the object, in this case ts0.
(4) Negate TimeSpan
Private Sub NegateTimeSpan()
Dim ts0 = New TimeSpan(0, 23, 59, 59, 0)
Dim ts1 As TimeSpan
ts1 = ts0.Negate
Console.WriteLine(ts0)
Console.WriteLine(ts1)
End Sub
Output:
23:59:59
-23:59:59
TimeSpan.Negate just changes the sign on
the input time span. Positive becomes negative
and negative becomes positive
(5) Adding TimeSpans
Private Sub TimeSpanAdd()
Dim sdx As String
Dim ts0 As New TimeSpan(0, 9, 30, 0)
Dim ts1 As New TimeSpan(0, 2, 30, 0)
Dim ts2 As TimeSpan
ts2 = ts0.Add(ts1)
sdx = String.Format("{0} add {1} = {2}", ts0, ts1, ts2)
Console.WriteLine(sdx)
ts2 = ts0 + ts1
sdx = String.Format("{0} + {1} = {2}", ts0, ts1, ts2)
Console.WriteLine(sdx)
End Sub
Output:
09:30:00 add 02:30:00 = 12:00:00
09:30:00 + 02:30:00 = 12:00:00
TimeSpan provides two ways to add times.
the .Add function and the + operator. It's
a matter of taste
(6) TimeSpan.Duration
Private Sub TimeSpanDuration()
Dim sdx As String
Dim ts0 As New TimeSpan(0, 9, 30, 0)
Dim ts1, ts2 As TimeSpan
ts1 = ts0.Negate '<=====negate to show result of .Duration=====>
'<=====obtain .Duration which is absolute value of the TimeSpan=====>
ts2 = ts0.Duration
sdx = String.Format("ts1 = {0} ===== ts2 = {1}", ts1, ts2)
Console.WriteLine(sdx)
End Sub
Output:
ts1 = -09:30:00 ===== ts2 = 09:30:00
So .Duration just returns the absolute value of the
time span
(7) Breakdown of TimeSpan parts
Private Sub TimeSpanParts()
Dim sdx As String
Dim ts0 As New TimeSpan(23, 10, 15, 33)
Dim ts1 As New TimeSpan(4, 6, 27, 15)
Dim ts2 As TimeSpan
ts2 = ts0 - ts1
sdx = String.Format("{0} Days {1} Hours {2} Minutes {3} Seconds", ts2.Days, ts2.Hours, ts2.Minutes, ts2.Seconds)
Console.Write(sdx)
End Sub
Output:
19 Days 3 Hours 48 Minutes 18 Seconds
In previous examples I have been displaying TimeSpans
as a whole unit such as: 74.01:46:40 meaning
days.hours:minutes:seconds. This just show that
individual elements are available with the TimeSpan
structure. There are many more
600
1200
1800
2400
3000
3600
4200
4800
5400
6000
6600
7200
7800
8400
9000
9600
10200
10800
11400
12000
12600
13200
13800
14400
15000
15600
16200
16800
17400
18000
18600
19200
19800
20400