System.TimeSpan

This structure represents an interval of time. Because the number of days in months and years varies and those values change between some cultures, the longest unit of time used by TimeSpan is a day. Listing B.16 shows an example of using TimeSpan (timespan.cs).

Listing B.16. TimeSpan Example
public class TimeSpanMain
{
    static void Main(string [] args)
    {
        // Allocate a TimeSpan object
        // with a given number of ticks
        TimeSpan ts = new TimeSpan(1012);
        Console.WriteLine("{0} ", ts);
        // Allocate a TimeSpan object
        // with days, hours, minutes, seconds
        ts = new TimeSpan(11, 13, 46, 40);
        Console.WriteLine("{0}  {1} ", ts, ts.Ticks.ToString("#,#"));
    }
}

The output for Listing B.16 is as follows:

 00:00:00.0001012 11.13:46:40 10,000,000,000,000 ...

Get .NET Common Language Runtime Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.