TimeSpan Subtracting


using System;
using System.Text;
class Sample
{
    public static void Main()
    {
        DateTime dt = new DateTime(2000, 2, 3, 10, 20, 30);


        TimeSpan ts = TimeSpan.FromMinutes(90);
        Console.WriteLine(dt.Add(ts));
        Console.WriteLine(dt - ts);  
    }
}

The output:


2/3/2000 11:50:30 AM
2/3/2000 8:50:30 AM

Subtract a TimeSpan from a DateTimeOffset


using System;
using System.Text;
class Sample
{
    public static void Main()
    {
        DateTimeOffset dt = new DateTimeOffset(2000, 2, 3, 10, 20, 30,new TimeSpan());


        TimeSpan ts = TimeSpan.FromMinutes(90);
        Console.WriteLine(dt.Add(ts));
        Console.WriteLine(dt - ts);  
    }
}

The output:


2/3/2000 11:50:30 AM +00:00
2/3/2000 8:50:30 AM +00:00

Subtract one DateTime/DateTimeOffset from another gives you a TimeSpan


using System;
using System.Text;
class Sample
{
    public static void Main()
    {
        DateTime thisYear = new DateTime(2007, 1, 1);
        DateTime nextYear = thisYear.AddYears(1);
        TimeSpan oneYear = nextYear - thisYear;
    }
}
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.