O'Reilly logo

.NET Common Language Runtime Unleashed by Kevin Burton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required