C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
StaticDoubleRegions: The five sets of four functions are all contained in separate #region blocks. We can collapse the regions in Visual Studio.
region, endregionNote: 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
}
Main: We call 5 of the conversion methods. The result of the methods is then printed to the Console.
ConsoleC# 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));
}
}