TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

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.

StaticDouble

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.

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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf