Suppose you have a value in milliseconds, and want to convert it to seconds or minutes for more readable output. In VB.NET the TimeSpan
type can be used for this.
With functions like TimeSpan.FromMilliseconds
or FromSeconds
, we can use .NET to convert these values. The code is built into .NET, so it is well-tested.
We introduce 20 VB.NET functions in the Module
here. For each function, we return a Double
that is calculated by a TimeSpan
shared function.
TimeSpan
calls.Main
function, we test the Functions. We determine that 100 hours equals 6000 minutes, which makes sense.Module Module1 Public Function ConvertMillisecondsToDays(milliseconds As Double) As Double Return TimeSpan.FromMilliseconds(milliseconds).TotalDays End Function Public Function ConvertSecondsToDays(seconds As Double) As Double Return TimeSpan.FromSeconds(seconds).TotalDays End Function Public Function ConvertMinutesToDays(minutes As Double) As Double Return TimeSpan.FromMinutes(minutes).TotalDays End Function Public Function ConvertHoursToDays(hours As Double) As Double Return TimeSpan.FromHours(hours).TotalDays End Function Public Function ConvertMillisecondsToHours(milliseconds As Double) As Double Return TimeSpan.FromMilliseconds(milliseconds).TotalHours End Function Public Function ConvertSecondsToHours(seconds As Double) As Double Return TimeSpan.FromSeconds(seconds).TotalHours End Function Public Function ConvertMinutesToHours(minutes As Double) As Double Return TimeSpan.FromMinutes(minutes).TotalHours End Function Public Function ConvertDaysToHours(days As Double) As Double Return TimeSpan.FromHours(days).TotalHours End Function Public Function ConvertMillisecondsToMinutes(milliseconds As Double) As Double Return TimeSpan.FromMilliseconds(milliseconds).TotalMinutes End Function Public Function ConvertSecondsToMinutes(seconds As Double) As Double Return TimeSpan.FromSeconds(seconds).TotalMinutes End Function Public Function ConvertHoursToMinutes(hours As Double) As Double Return TimeSpan.FromHours(hours).TotalMinutes End Function Public Function ConvertDaysToMinutes(days As Double) As Double Return TimeSpan.FromDays(days).TotalMinutes End Function Public Function ConvertMillisecondsToSeconds(milliseconds As Double) As Double Return TimeSpan.FromMilliseconds(milliseconds).TotalSeconds End Function Public Function ConvertMinutesToSeconds(minutes As Double) As Double Return TimeSpan.FromMinutes(minutes).TotalSeconds End Function Public Function ConvertHoursToSeconds(hours As Double) As Double Return TimeSpan.FromHours(hours).TotalSeconds End Function Public Function ConvertDaysToSeconds(days As Double) As Double Return TimeSpan.FromDays(days).TotalSeconds End Function Public Function ConvertSecondsToMilliseconds(seconds As Double) As Double Return TimeSpan.FromSeconds(seconds).TotalMilliseconds End Function Public Function ConvertMinutesToMilliseconds(minutes As Double) As Double Return TimeSpan.FromMinutes(minutes).TotalMilliseconds End Function Public Function ConvertHoursToMilliseconds(hours As Double) As Double Return TimeSpan.FromHours(hours).TotalMilliseconds End Function Public Function ConvertDaysToMilliseconds(days As Double) As Double Return TimeSpan.FromDays(days).TotalMilliseconds End Function Sub Main() ' 500000 milliseconds = 0.00578703704 days Console.WriteLine(ConvertMillisecondsToDays(500000)) ' 100 hours = 6000 minutes Console.WriteLine(ConvertHoursToMinutes(100)) ' 10000 days = 240000 hours Console.WriteLine(ConvertDaysToHours(10000)) ' 500 minutes = 8.33333333 hours Console.WriteLine(ConvertMinutesToHours(500)) ' 600000 milliseconds = 600 seconds Console.WriteLine(ConvertMillisecondsToSeconds(600000)) End Sub End Module0.005787037037037037 6000 10000 8.333333333333334 600
By using properties like TotalMinutes
on the TimeSpan
type, we can use TimeSpan
to perform conversions. This is a convenient, and reliable, approach to these conversions.
A key benefit to these functions is that you do not have to rely on the competence of people posting content to the Internet. The functions are built into .NET, so should be more reliable.