This documentation is archived and is not being maintained.

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)

TimeSpan Add(
	TimeSpan 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.


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 creates several pairs of TimeSpan objects and calculates their sum with the Add method.

// Example of the TimeSpan::Add( ) and TimeSpan::Subtract( ) methods.
using namespace System;

// Pad the end of a TimeSpan string with spaces if it does not 
// contain milliseconds.
String^ Align( TimeSpan interval )
   String^ intervalStr = interval.ToString();
   int pointIndex = intervalStr->IndexOf( ':' );
   pointIndex = intervalStr->IndexOf( '.', pointIndex );
   if ( pointIndex < 0 )
      intervalStr = String::Concat( intervalStr, "        " );

   return intervalStr;

// Display TimeSpan parameters and their sum and difference.
static void ShowTimeSpanSumDiff( TimeSpan Left, TimeSpan Right )
   String^ dataFmt = "{0,-24}{1,24}";
   Console::WriteLine( dataFmt, "TimeSpan Left", Align( Left ) );
   Console::WriteLine( dataFmt, "TimeSpan Right", Align( Right ) );
   Console::WriteLine( dataFmt, "Left.Add( Right )", Align( Left.Add( Right ) ) );
   Console::WriteLine( dataFmt, "Left.Subtract( Right )", Align( Left.Subtract( Right ) ) );

int main()
   Console::WriteLine( "This example of the TimeSpan::Add( ) and "
   "TimeSpan::Subtract( ) \nmethods generates the "
   "following output by creating several \npairs of "
   "TimeSpan objects and calculating and displaying \n"
   "the sum and difference of each." );

   // Create pairs of TimeSpan objects.
   ShowTimeSpanSumDiff( TimeSpan(1,20,0), TimeSpan(0,45,10) );
   ShowTimeSpanSumDiff( TimeSpan(1,10,20,30,40), TimeSpan( -1,2,3,4,5) );
   ShowTimeSpanSumDiff( TimeSpan(182,12,30,30,505), TimeSpan(182,11,29,29,495) );
   ShowTimeSpanSumDiff( TimeSpan(888888888888888), TimeSpan(999999999999999) );

This example of the TimeSpan::Add( ) and TimeSpan::Subtract( )
methods generates the following output by creating several
pairs of TimeSpan objects and calculating and displaying
the sum and difference of each.

TimeSpan Left                   01:20:00
TimeSpan Right                  00:45:10
Left.Add( Right )               02:05:10
Left.Subtract( Right )          00:34:50

TimeSpan Left                 1.10:20:30.0400000
TimeSpan Right                 -21:56:55.9950000
Left.Add( Right )               12:23:34.0450000
Left.Subtract( Right )        2.08:17:26.0350000

TimeSpan Left               182.12:30:30.5050000
TimeSpan Right              182.11:29:29.4950000
Left.Add( Right )           365.00:00:00
Left.Subtract( Right )          01:01:01.0100000

TimeSpan Left              1028.19:21:28.8888888
TimeSpan Right             1157.09:46:39.9999999
Left.Add( Right )          2186.05:08:08.8888887
Left.Subtract( Right )     -128.14:25:11.1111111

.NET Framework

Supported in: 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

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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