C# : .NET

[".0s4*0|collections;datetime-format;collections",["F@eA","KABEBELABEKABEMAJA","OBOCSTUUUUTTUUUUYHDHDHDHDHDHDHDHDHDHDHDHDHDHDHDHDHDHDHDHDOBBOBCCOPGDGDGDGDGDOBBCCCCOOBCWCCSTTUUUUTTUUUU","twsl....rsh.",".NET","Array","Dictionary","List","String","2D","Async","Console","DataTable","Dates","DateTime","Enum","File","For","Foreach","Format","IEnumerable","If","IndexOf","Lambda","LINQ","Optimization","Parse","Path","Process","Property","Random","Regex","Replace","Sort","Split","Static","Substring","Switch","Tuple","While","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. We convert them to other time units. We look at 20 methods that use TimeSpan. ","TimeSpan ","timespan","Example."," 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.","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","C# program that converts small time units","\n\nusing System;\n\npublic static class TimeSpanUtil\n{\n #region To days\n public static double ","ConvertMillisecondsToDays","(double milliseconds)\n {\n return TimeSpan.FromMilliseconds(milliseconds).TotalDays;\n }\n\n public static double ","ConvertSecondsToDays","(double seconds)\n {\n return TimeSpan.FromSeconds(seconds).TotalDays;\n }\n\n public static double ","ConvertMinutesToDays","(double minutes)\n {\n return TimeSpan.FromMinutes(minutes).TotalDays;\n }\n\n public static double ","ConvertHoursToDays","(double hours)\n {\n return TimeSpan.FromHours(hours).TotalDays;\n }\n #endregion\n\n #region To hours\n public static double ","ConvertMillisecondsToHours","(double milliseconds)\n {\n return TimeSpan.FromMilliseconds(milliseconds).TotalHours;\n }\n\n public static double ","ConvertSecondsToHours","(double seconds)\n {\n return TimeSpan.FromSeconds(seconds).TotalHours;\n }\n\n public static double ","ConvertMinutesToHours","(double minutes)\n {\n return TimeSpan.FromMinutes(minutes).TotalHours;\n }\n\n public static double ","ConvertDaysToHours","(double days)\n {\n return TimeSpan.FromHours(days).TotalHours;\n }\n #endregion\n\n #region To minutes\n public static double ","ConvertMillisecondsToMinutes","(double milliseconds)\n {\n return TimeSpan.FromMilliseconds(milliseconds).TotalMinutes;\n }\n\n public static double ","ConvertSecondsToMinutes","(double seconds)\n {\n return TimeSpan.FromSeconds(seconds).TotalMinutes;\n }\n\n public static double ","ConvertHoursToMinutes","(double hours)\n {\n return TimeSpan.FromHours(hours).TotalMinutes;\n }\n\n public static double ","ConvertDaysToMinutes","(double days)\n {\n return TimeSpan.FromDays(days).TotalMinutes;\n }\n #endregion\n\n #region To seconds\n public static double ","ConvertMillisecondsToSeconds","(double milliseconds)\n {\n return TimeSpan.FromMilliseconds(milliseconds).TotalSeconds;\n }\n\n public static double ","ConvertMinutesToSeconds","(double minutes)\n {\n return TimeSpan.FromMinutes(minutes).TotalSeconds;\n }\n\n public static double ","ConvertHoursToSeconds","(double hours)\n {\n return TimeSpan.FromHours(hours).TotalSeconds;\n }\n\n public static double ","ConvertDaysToSeconds","(double days)\n {\n return TimeSpan.FromDays(days).TotalSeconds;\n }\n #endregion\n\n #region To milliseconds\n public static double ","ConvertSecondsToMilliseconds","(double seconds)\n {\n return TimeSpan.FromSeconds(seconds).TotalMilliseconds;\n }\n\n public static double ","ConvertMinutesToMilliseconds","(double minutes)\n {\n return TimeSpan.FromMinutes(minutes).TotalMilliseconds;\n }\n\n public static double ","ConvertHoursToMilliseconds","(double hours)\n {\n return TimeSpan.FromHours(hours).TotalMilliseconds;\n }\n\n public static double ","ConvertDaysToMilliseconds","(double days)\n {\n return TimeSpan.FromDays(days).TotalMilliseconds;\n }\n #endregion\n}","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 Class ","static","Double Type ","double","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 ","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. ","C# program that demonstrates the class","\n\nusing System;\nusing T = TimeSpanUtil;\n\nclass Program\n{\n static void Main()\n {","\n // 500000 milliseconds = 0.00578703704 days\n ","Console.WriteLine(T.ConvertMillisecondsToDays(500000));","\n\n // 100 hours = 6000 minutes\n ","Console.WriteLine(T.ConvertHoursToMinutes(100));","\n\n // 10000 days = 240000 hours\n ","Console.WriteLine(T.ConvertDaysToHours(10000));","\n\n // 500 minutes = 8.33333333 hours\n ","Console.WriteLine(T.ConvertMinutesToHours(500));","\n\n // 600000 milliseconds = 600 seconds\n ","Console.WriteLine(T.ConvertMillisecondsToSeconds(600000));\n }\n}","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, Args ","main","Console.WriteLine ","console","Convert milliseconds to days: ","You can check Google to see that 500000 milliseconds equals 0.005 days. That's not a long time.","Hours to minutes: ","You can check Google to see 100 hours are equal to 6 thousand minutes. That's a much longer time period.","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.","Minutes to hours: ","This does the opposite of the first example. 500 minutes is 8.3 hours\u2014a good work day.","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.","Alias syntax."," 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 ","using-alias","And: ","Typing TimeSpanUtil many times might be harder to read. This could reduce code quality.","Summary."," 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.","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"],"url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABPCAMAAACZM3rMAAAAYFBMVEX/////75TU1NTExMRvb2/u7u709PTt5K7Ly8uqqqp2dnaEhITl37vg28Lz6KX67Jy4uLjj4+P97pfR0dHX19ecnJz+/v78/Pz+75Xb29uRkZHk3r3b2Mr4+Pji3b/W1dBwG/StAAAC/UlEQVR4Xt3aCXLiMBCG0dbqfd9YM/e/5cRUF3JKgYlGdtvKdwL+1CNAA7ilFRClNGyY4g0Q1XAFWzU1nHIIbybYJH3mfyiH/OFnvQ0rXkSUQ6KCc7UJq5zRDmH5+rzun6wiRj2ERZ+87muzqtg8JAai4nkIqwreq1VZXdlcxAUQJXjE5q7IazVWOw1ZkZfqZ1Z7DVmN1zizOrE9h7DTzGv0ZoVW9xqCEpCXH6vdhyAvP1YtO8IQ1iIvD1Y7D/HnpXt+q9hxhrDqxnv9X6w+WnakIaz9QF6OrHJmVfJYERXzklnlyMuJVcnsTjdO1u3E7Erk5cEKl0Q5URHu8OA1Iatjhh9SPFjtmzsvhayOGvJSP/pEiwXMS589WBHzOuv3rCoWRBXyImFFz8tmFS4vPJQEVYUXPAJWtLzwrFuyACvxQEzAioaXzSpcXnh/Cza84IXLyjqxqP53DPkttMJ+slvn+rgL+d9vF+OM+yAzEe4Losjk8JDVZFLKtA71LUqdSimzBiBO5aMBeQXGapCP0hgEDpFZHN7b+DiTOEQgLRkeL2T1KGvwyY4NXUgfdTvzuPGMavEKjhVm8QqOlUkbXmEc6AwrDV8ahQyEF7LCxAgYAS8KVq682uiDqKh1ZWUaa4ldXvBqKb/oaV+wukisHuFV5ycv8eKrt/pCVM3Lf7A6w5v0c27y/ZehiSQq+f7L0OQ9K9P0hVd1pCGVxep9fbrgFR1nSMS5eD60Hn6QWvK6tscY0l4XrC4KMAdeRXWEIVWxZDXBT+PPVxzktecQZPV8veaAufM6mSG1IKo2Q07urEyTAdnt/1PAzvxdJ3CtM7z2/nGmYdWByZ1Xmuz7c9kktVh58SIf4sDKjRf1EH9WJpUteOXUQ5asMgVe3ZPFiaWgHVIsDiXJHXyLl7wohyxZxeCVfWIhHGIdSvxbXvDohlj3N6+sCx7dEPv+tiqvBIhKVmZleFEPWZ2V4UU/xLBalxfpEGS1RXqgHTJo2KhRUA4RI2xXUwNRtSOrv73beOZ5KrZYAAAAAElFTkSuQmCC)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAACCAgMAAADasxWRAAAADFBMVEXZ7/////+VlZX29vYl5th1AAAASElEQVR4AWPACZj/f0DmjkqOSjKtQgcLyJccZiEE9SOmP8mXHEaBMBoIo4EwGggryJccvvVKKAogTnJUclRyVHLklgmjkqOSAALWyrsxEHLDAAAAAElFTkSuQmCC)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAACCAgMAAADasxWRAAAADFBMVEX/6Nn///+VlZX29va5ksB5AAAASElEQVR4AWPACZj/f0DmjkqOSjKtQgcLyJccZiEE9SOmP8mXHEaBMBoIo4EwGggryJccvvVKKAogTnJUclRyVHLklgmjkqOSAALWyrsxEHLDAAAAAElFTkSuQmCC)"]

["url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANIAAABCCAMAAAAVIXgzAAAAMFBMVEX/////6u7/3+XOwf/0xf+7v//y3v/w8f//2eb/+vX/59T/ztzk5///9O7/4tz/6uVDuxD0AAAAvElEQVR42u3YNw4DMQwFUSptoMPe/7YWILJi4zXUWJg5wAdepSArt7dRUU96zVLv/HIrTkmcmrgFCRIkSJAgQYIECRIkSLO3n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