Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TimeSpan.Add Method

Returns a new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public TimeSpan Add(
	TimeSpan ts
)

Parameters

ts
Type: System.TimeSpan

The time interval to add.

Return Value

Type: System.TimeSpan
A new object that represents the value of this instance plus the value of ts.

ExceptionCondition
OverflowException

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

The return value must be between TimeSpan.MinValue and TimeSpan.MaxValue; otherwise, an exception is thrown.

The return value is a new TimeSpan; the original TimeSpan is not modified.

The following example calls the Add method to add each element in an array of time intervals to a base TimeSpan value.

using System;

public class Example
{
   public static void Main()
   {
      TimeSpan baseTimeSpan = new TimeSpan(1, 12, 15, 16);
      // Create an array of timespan intervals.
      TimeSpan[] intervals = { TimeSpan.FromDays(1.5), 
                               TimeSpan.FromHours(1.5), 
                               TimeSpan.FromMinutes(45), 
                               TimeSpan.FromMilliseconds(505),
                               new TimeSpan(1, 17, 32, 20), 
                               new TimeSpan(-8, 30, 0) };
      // Calculate a new time interval by adding each element to the base interval. 
      foreach (var interval in intervals)
         Console.WriteLine(@"{0,-10:g} {3} {1,15:%d\:hh\:mm\:ss\.ffff} = {2:%d\:hh\:mm\:ss\.ffff}", 
                           baseTimeSpan, interval, baseTimeSpan.Add(interval), 
                           interval < TimeSpan.Zero ? "-" : "+");
   }
}
// The example displays the following output: 
//       1:12:15:16 + 1:12:00:00.0000 = 3:00:15:16.0000 
//       1:12:15:16 + 0:01:30:00.0000 = 1:13:45:16.0000 
//       1:12:15:16 + 0:00:45:00.0000 = 1:13:00:16.0000 
//       1:12:15:16 + 0:00:00:00.5050 = 1:12:15:16.5050 
//       1:12:15:16 + 1:17:32:20.0000 = 3:05:47:36.0000 
//       1:12:15:16 - 0:07:30:00.0000 = 1:04:45:16.0000

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft