VB.NET TimeSpan

TimeSpan

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

We can create a new TimeSpan instance in another way. 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, Subtract

Plus

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.

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

Note

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.

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

Question

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).

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

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.

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

Programming tip

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.

Tip:Please note that the other Total properties on TimeSpan 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.

Tip:They are the easiest way to represent time periods in the VB.NET language. Understanding them and how to use them is worthwhile.


VB.NET: Time