Convert Milliseconds, Seconds, MinutesUse TimeSpan to correctly convert values in milliseconds, seconds, minutes and hours.
This page was last reviewed on Feb 23, 2024.
Convert milliseconds. 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.
Example. 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.
Tip It might be better to just reuse the internal statements of the functions—just the TimeSpan calls.
Result In the 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 Module
0.005787037037037037 6000 10000 8.333333333333334 600
Summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 23, 2024 (new).
© 2007-2024 Sam Allen.