C# Convert Milliseconds, Seconds, Minutes

Convert time values including days, hours, minutes and seconds.

Convert milliseconds. Milliseconds, seconds, minutes, hours and days can be converted. These values may be from another data source such as a database or file from a different system.Convert

Notes, TimeSpan. We convert these time units to other time units. We look at 20 methods that use TimeSpan, a class that can help us perform these conversions.TimeSpan

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.

Tip: Because they use these .NET Framework methods, you can quickly see they are correct in code review.

Info: The class contains 20 methods, organized by their numeric units. All the parameters are doubles, and the return values are doubles.


Regions: The five sets of four functions are all contained in separate #region blocks. We can collapse the regions in Visual Studio.

region, endregion

Note: These are methods that simply return the value of a more complex expression using TimeSpan.From methods.

C# program that converts small time units using System; 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 }

Example 2. Here we convert milliseconds, seconds, minutes, hours, and days to different units. The TimeSpanUtil class, with its 20 public static methods, performs these conversions.

Main: We call 5 of the conversion methods. The result of the methods is then printed to the Console.

C# program that demonstrates TimeSpanUtil using System; using T = TimeSpanUtil; 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)); } }

Notes, testing. We can test the results against an Internet search. But given the .NET Framework provides these methods, we can rely on its own testing.

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 (ms), seconds (s), 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
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to