Convert Milliseconds, Seconds, Minutes
This page was last reviewed on Aug 10, 2021.
Dot Net Perls
Convert milliseconds. With C#, milliseconds, seconds, minutes, hours and days can be converted. These values may be from another data source (like a database or file).
TimeSpan notes. We convert these time units to other time units. We look at C# methods that use TimeSpan, a class that can help us perform these conversions.
Convert Nanoseconds
An example. The number we are converting is a length of time, not a specific time. These methods internally call TimeSpan.FromMilliseconds, FromSeconds, FromMinutes, FromHours and FromDays.
Info The class contains 20 methods, organized by their numeric units. All the parameters are doubles, and the return values are doubles.
Detail The 5 sets of 4 functions are all contained in separate #region blocks. We can collapse the regions in Visual Studio.
Note These are methods that simply return the value of a more complex expression using TimeSpan.From methods.
using System; using T = TimeSpanUtil; public static class TimeSpanUtil { #region To days public static double ConvertMillisecondsToDays(double milliseconds) { return TimeSpan.FromMilliseconds(milliseconds).TotalDays; } public static double ConvertSecondsToDays(double seconds) { return TimeSpan.FromSeconds(seconds).TotalDays; } public static double ConvertMinutesToDays(double minutes) { return TimeSpan.FromMinutes(minutes).TotalDays; } public static double ConvertHoursToDays(double hours) { return TimeSpan.FromHours(hours).TotalDays; } #endregion #region To hours public static double ConvertMillisecondsToHours(double milliseconds) { return TimeSpan.FromMilliseconds(milliseconds).TotalHours; } public static double ConvertSecondsToHours(double seconds) { return TimeSpan.FromSeconds(seconds).TotalHours; } public static double ConvertMinutesToHours(double minutes) { return TimeSpan.FromMinutes(minutes).TotalHours; } public static double ConvertDaysToHours(double days) { return TimeSpan.FromHours(days).TotalHours; } #endregion #region To minutes public static double ConvertMillisecondsToMinutes(double milliseconds) { return TimeSpan.FromMilliseconds(milliseconds).TotalMinutes; } public static double ConvertSecondsToMinutes(double seconds) { return TimeSpan.FromSeconds(seconds).TotalMinutes; } public static double ConvertHoursToMinutes(double hours) { return TimeSpan.FromHours(hours).TotalMinutes; } public static double ConvertDaysToMinutes(double days) { return TimeSpan.FromDays(days).TotalMinutes; } #endregion #region To seconds public static double ConvertMillisecondsToSeconds(double milliseconds) { return TimeSpan.FromMilliseconds(milliseconds).TotalSeconds; } public static double ConvertMinutesToSeconds(double minutes) { return TimeSpan.FromMinutes(minutes).TotalSeconds; } public static double ConvertHoursToSeconds(double hours) { return TimeSpan.FromHours(hours).TotalSeconds; } public static double ConvertDaysToSeconds(double days) { return TimeSpan.FromDays(days).TotalSeconds; } #endregion #region To milliseconds public static double ConvertSecondsToMilliseconds(double seconds) { return TimeSpan.FromSeconds(seconds).TotalMilliseconds; } public static double ConvertMinutesToMilliseconds(double minutes) { return TimeSpan.FromMinutes(minutes).TotalMilliseconds; } public static double ConvertHoursToMilliseconds(double hours) { return TimeSpan.FromHours(hours).TotalMilliseconds; } public static double ConvertDaysToMilliseconds(double days) { return TimeSpan.FromDays(days).TotalMilliseconds; } #endregion } class Program { static void Main() { // 500000 milliseconds = 0.00578703704 days Console.WriteLine(T.ConvertMillisecondsToDays(500000)); // 100 hours = 6000 minutes Console.WriteLine(T.ConvertHoursToMinutes(100)); // 10000 days = 240000 hours Console.WriteLine(T.ConvertDaysToHours(10000)); // 500 minutes = 8.33333333 hours Console.WriteLine(T.ConvertMinutesToHours(500)); // 600000 milliseconds = 600 seconds Console.WriteLine(T.ConvertMillisecondsToSeconds(600000)); } }
0.005787037037037037 6000 10000 8.333333333333334 600
Notes, testing. We can test the results against an Internet search. But given .NET provides these methods, we can rely on its own testing.
Info We call 5 of the conversion methods. The result of the methods is then printed to the Console.
Notes, alias syntax. We can use the type alias syntax in the C# language. Many developers dislike this syntax. But this is a good usage of it as it shortens the example.
using Alias
We converted milliseconds, seconds, minutes, hours, and days to one another. This is useful for databases (as an example) where the time periods are stored in milliseconds.
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 Aug 10, 2021 (image).
© 2007-2024 Sam Allen.