Edit

Share via


TimeSpan.Addition(TimeSpan, TimeSpan) Operator

Definition

Adds two specified TimeSpan instances.

C#
public static TimeSpan operator +(TimeSpan t1, TimeSpan t2);

Parameters

t1
TimeSpan

The first time interval to add.

t2
TimeSpan

The second time interval to add.

Returns

An object whose value is the sum of the values of t1 and t2.

Exceptions

The resulting TimeSpan is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.

Remarks

The Addition method defines the addition operator for TimeSpan values. It enables code such as the following:

C#
TimeSpan time1 = new TimeSpan(1, 0, 0, 0);   // TimeSpan equivalent to 1 day.
TimeSpan time2 = new TimeSpan(12, 0, 0);     // TimeSpan equivalent to 1/2 day.
TimeSpan time3 = time1 + time2;              // Add the two time spans.

Console.WriteLine("  {0,12}\n +  {1,10}\n   {3}\n    {2,10}", 
                  time1, time2, time3, new String('_', 10));

// The example displays the following output:
//           1.00:00:00
//        +    12:00:00
//          __________
//           1.12:00:00

Languages that do not support custom operators can call the Add method instead.

The equivalent method for this operator is TimeSpan.Add(TimeSpan)

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also