VB.NET TimeSpan Examples

TimeSpan conceptual illustration

You need to use the TimeSpan type in your VB.NET program to manipulate or represent periods of time. With TimeSpan, you can use a host of helper functions to make managing time periods easier.

This VB tutorial demonstrates how to use TimeSpan. It provides example programs.

TimeSpan constructor

The TimeSpan type has a lot of very 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 minutes.

Program that uses TimeSpan constructor [VB.NET]

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 functions

There is another way you can create a new TimeSpan instance. The From functions allow you to specify a specific unit to create the TimeSpan from. So you can specify days, hours, minutes, seconds or milliseconds and get a TimeSpan.

Program that uses From functions [VB.NET]

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 and Subtract

Another useful feature of the TimeSpan type is that you can add and subtract instances. This allows you to compute the total time or the difference between two TimeSpan instances. You can see how much larger one TimeSpan is than another.

Program that uses Add/Subtract [VB.NET]

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 and Zero

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. In many programs, you can use the TimeSpan.Zero constant as a default value for a TimeSpan.

Program that uses Max, Min and Zero [VB.NET]

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 constants

Question and answer

How can you convert ticks to other units of time such as days, hours, minutes, seconds or milliseconds? You can use the TimeSpan.TicksPer constants. For example, 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).

Program that uses TicksPer constants [VB.NET]

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 function

Sometimes it is necessary to convert a negative TimeSpan into a positive TimeSpan. For example, if you subtract a larger TimeSpan from a smaller TimeSpan, your result will be negative. You can then invoke Duration() on that value to see the difference.

Program that uses Duration function [VB.NET]

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 versus TotalHours

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. Please note that the other Total properties follow this same pattern.

Program that uses Hours and TotalHours [VB.NET]

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

The VB.NET programming language

This example set provided a walkthrough of the TimeSpan type in the VB.NET programming language and the .NET Framework. TimeSpans are extremely useful in a wide variety of programs. They are the easiest way to represent time periods in the VB.NET language. Understanding them and how to use them is a very valuable skill.

VB.NET Tutorials
.NET