TimeSpan Adding

The + operator is overloaded to do the same job:


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 11:50:30 AM

The Add method adds a TimeSpan to 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 11:50:30 AM +00:00
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.