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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Aug 10, 2021 (image).