Convert milliseconds. With C#, milliseconds, seconds, minutes, hours and days can be converted. These values may be from another data source (like a database or file).
TimeSpan notes. We convert these time units to other time units. We look at C# methods that use TimeSpan, a class that can help us perform these conversions.
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.
Info The class contains 20 methods, organized by their numeric units. All the parameters are doubles, and the return values are doubles.
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.
We converted milliseconds, seconds, 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 is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 10, 2021 (image).