C# Convert Milliseconds

Convert or change

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. We convert them to other time units. We look at 20 methods that use TimeSpan.

TimeSpan

Example

Note

First, the number we are converting is a length of time, not a specific time. These methods all 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.

Program that converts small time units: C#

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
}

We see a public static class. It contains 20 methods, organized by the result numeric unit, and then by the input numeric unit. All the parameters are doubles, and the return values are doubles.

Static ClassDouble TypePound symbol

Using #region and #endregion. The five sets of four functions are all contained in separate #region blocks. This enhances code clarity and you can collapse the regions in Visual Studio.

Region

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

Tip:The methods will be inlined usually, meaning the actual functions will not affect the performance of the execution engine.

Example 2

Here we convert milliseconds, seconds, minutes, hours, and days to different units.
The above class,
with its 20 public static methods,
perform these conversions. The conversions shown here are not a complete test of the class.

Program that demonstrates the class: C#

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));
    }
}
Main method

This class defines the Main entry point for your program. Internally, it calls five of the convert methods defined at the top of this document. The result of the methods is then printed to the Console.

Main, ArgsConsoleGoogle search engine

Convert milliseconds to days:You can check Google to see that 500000 milliseconds equals 0.005 days. That's not a long time.

Google search 1

Hours to minutes:You can check Google to see 100 hours are equal to 6 thousand minutes. That's a much longer time period.

Google search 2

Days to hours:Here we see that ten thousand days is equal to 240 thousand hours. I am reasonably certain I will live that long.

Google search 3

Minutes to hours:This does the opposite of the first example. 500 minutes is 8.3 hours—a good work day.

Google search 4

Milliseconds to seconds. This converts six-hundred-thousand milliseconds to seconds. The result is 600 seconds, which makes perfect sense to me. I can even handle this kind of math.

Google search 5

Alias syntax

Using keyword

Finally, you should see that the second line of the program uses 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 Example

And:Typing TimeSpanUtil many times might be harder to read. This could reduce code quality.

Summary

Framework: NET

Here we saw ways to convert milliseconds (ms), seconds (s), minutes, hours, and days to one another. This is useful for databases where the time periods are stored in milliseconds, for example.

Note:Some ASP.NET sites will store the length of sessions in milliseconds, but you may want to present it in minutes.

Also:We checked Google for the correct values of our methods. The code here uses .NET Framework methods. It is easy to verify.


C#: Cast