TimeSpan operators

C# overloads <,>, +,-, for TimeSpan type.

The following expression evaluates to a TimeSpan of 2.5 hours:


using System;
using System.Text;
class Sample
{
    public static void Main()
    {
        TimeSpan ts = TimeSpan.FromHours(2) + TimeSpan.FromMinutes(30);
        Console.WriteLine(ts);
    }
}

The output:


02:30:00

The following expression evaluates to one second short of 10 days:


using System;
using System.Text;
class Sample
{
    public static void Main()
    {
        TimeSpan ts = TimeSpan.FromDays(10) - TimeSpan.FromSeconds(1);  // 9.23:59:59

        Console.WriteLine(ts);
    }
}

The output:


9.23:59:59
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.