HomeSearch

VB.NET TimeSpan Examples

Use TimeSpan to indicate ranges of time in days, hours, and other units.
TimeSpan represents a period of time. With it we use a host of helper functions to make managing time periods easier. This leads to simpler, more reliable VB.NET programs that act upon time representations.
Constructor. The TimeSpan type has many useful constructors. These allow you to specify the exact period of time you want the TimeSpan to represent. In this example, we specify a TimeSpan of one day, two hours, and thirty seconds.Constructors

Tip: Arguments to the TimeSpan constructor are ordered from large stun it of time to smallest.

VB.NET program that uses constructor Module Module1 Sub Main() ' Use the TimeSpan constructor to specify... ' ... days, hours, minutes, seconds, milliseconds. Dim span As TimeSpan = New TimeSpan(1, 2, 0, 30, 0) Console.WriteLine(span) End Sub End Module Output 1.02:00:30
TimeSpan.From. We can create a new TimeSpan instance in another way. The From functions allow you to specify a unit to create the TimeSpan from. So you can specify days, hours, minutes, seconds or milliseconds and get a TimeSpan.
VB.NET program that uses From functions Module Module1 Sub Main() ' Get TimeSpan instances from a specific unit of time. Dim span1 As TimeSpan = TimeSpan.FromDays(1) Dim span2 As TimeSpan = TimeSpan.FromHours(1) Dim span3 As TimeSpan = TimeSpan.FromMinutes(1) Dim span4 As TimeSpan = TimeSpan.FromSeconds(1) Dim span5 As TimeSpan = TimeSpan.FromMilliseconds(1) Console.WriteLine(span1) Console.WriteLine(span2) Console.WriteLine(span3) Console.WriteLine(span4) Console.WriteLine(span5) End Sub End Module Output 1.00:00:00 01:00:00 00:01:00 00:00:01 00:00:00.0010000
Add, Subtract. The TimeSpan type allows us to add and subtract instances. We can compute the total time or the difference between two TimeSpan instances. We find how much larger one TimeSpan is than another.
VB.NET program that uses Add, Subtract Module Module1 Sub Main() ' Input TimeSpans. Dim span1 As TimeSpan = TimeSpan.FromMinutes(1) Dim span2 As TimeSpan = TimeSpan.FromMinutes(2) ' Add. Dim span3 As TimeSpan = span1.Add(span2) Console.WriteLine(span3) ' Subtract. Dim span4 As TimeSpan = span2.Subtract(span1) Console.WriteLine(span4) End Sub End Module Output 00:03:00 00:01:00
Max, Min. There are a lot of useful constants on the TimeSpan type as well. For example, there exists the MaxValue, MinValue, and Zero constants. These return a TimeSpan instance that has been initialized to a special value.

And: In many programs, you can use the TimeSpan.Zero constant as a default value for a TimeSpan.

VB.NET program that uses Max, Min and Zero Module Module1 Sub Main() ' Maximum, minimum, zero values. Console.WriteLine(TimeSpan.MaxValue) Console.WriteLine(TimeSpan.MinValue) Console.WriteLine(TimeSpan.Zero) End Sub End Module Output 10675199.02:48:05.4775807 -10675199.02:48:05.4775808 00:00:00
TicksPer. How can you convert ticks to other units of time? You can use the TimeSpan.TicksPer constants. If you have a figure in ticks and want to convert it to a minute, you can multiply it by TimeSpan.TicksPerMinute (which is 600000000).
VB.NET program that uses TicksPer constants Module Module1 Sub Main() ' Display these constants. Console.WriteLine(TimeSpan.TicksPerDay) Console.WriteLine(TimeSpan.TicksPerHour) Console.WriteLine(TimeSpan.TicksPerMinute) Console.WriteLine(TimeSpan.TicksPerSecond) Console.WriteLine(TimeSpan.TicksPerMillisecond) End Sub End Module Output 864000000000 36000000000 600000000 10000000 10000
Duration. Sometimes it is necessary to convert a negative TimeSpan into a positive one. For example, if you subtract a larger TimeSpan from a smaller TimeSpan, your result will be negative. You can then invoke Duration() to see the difference.
VB.NET program that uses Duration function Module Module1 Sub Main() ' Convert this negative TimeSpan into a positive TimeSpan. Dim span As TimeSpan = New TimeSpan(-1, -1, -1) Dim duration As TimeSpan = span.Duration() Console.WriteLine(duration) End Sub End Module Output 01:01:01
Hours. An important distinction exists between the Hours and TotalHours properties on the TimeSpan. The Hours returns the hours part of the TimeSpan. The TotalHours returns the entire TimeSpan represented by Hours.

Note: The other Total properties on TimeSpan follow this same pattern. They represent the total time, not just a unit of it.

VB.NET program that uses Hours and TotalHours Module Module1 Sub Main() ' Compare Hours and TotalHours. Dim span As TimeSpan = New TimeSpan(0, 500, 0, 0, 0) Console.WriteLine(span.Hours) Console.WriteLine(span.TotalHours) End Sub End Module Output 20 500
Summary. This example set provided a walk-through of the TimeSpan type in the VB.NET programming language and the .NET Framework. TimeSpans are extremely useful in a wide variety of programs.
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls