TimeSpan.Add Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Updated: October 2010
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Function Add ( _
ts As TimeSpan _
) As TimeSpan
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.
Exceptions
Exception | Condition |
---|---|
OverflowException | The resulting TimeSpan is less than MinValue or greater than MaxValue. |
Remarks
The return value must be between MinValue and MaxValue; otherwise, an exception is thrown.
The return value is a new TimeSpan; the original TimeSpan is not modified.
Examples
The following code example creates several pairs of TimeSpan objects and calculates their sum with the Add method.
' Example of the TimeSpan.Add( ) and TimeSpan.Subtract( ) methods.
Module Example
Const dataFmt As String = "{0,-24}{1,24}"
' Pad the end of a TimeSpan string with spaces if it does not
' contain milliseconds.
Function Align(ByVal interval As TimeSpan) As String
Dim intervalStr As String = interval.ToString()
Dim pointIndex As Integer = intervalStr.IndexOf(":"c)
pointIndex = intervalStr.IndexOf("."c, pointIndex)
If pointIndex < 0 Then intervalStr &= " "
Align = intervalStr
End Function
' Display TimeSpan parameters and their sum and difference.
Sub ShowTimeSpanSumDiff(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal Left As TimeSpan, ByVal Right As TimeSpan)
outputBlock.Text &= vbCrLf
outputBlock.Text &= String.Format(dataFmt, "TimeSpan Left", Align(Left)) & vbCrLf
outputBlock.Text &= String.Format(dataFmt, "TimeSpan Right", Align(Right)) & vbCrLf
outputBlock.Text &= String.Format(dataFmt, "Left.Add( Right ) & vbCrLf", _
Align(Left.Add(Right)))
outputBlock.Text &= String.Format(dataFmt, "Left.Subtract( Right ) & vbCrLf", _
Align(Left.Subtract(Right)))
End Sub
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
outputBlock.Text &= _
"This example of the TimeSpan.Add( ) and " & _
"TimeSpan.Subtract( ) " & vbCrLf & "methods " & _
"generates the following output by creating several " & _
vbCrLf & "pairs of TimeSpan objects and calculating " & _
"and displaying " & vbCrLf & "the sum " & _
"and difference of each." & vbCrLf
' Create pairs of TimeSpan objects.
ShowTimeSpanSumDiff(outputBlock, _
New TimeSpan(1, 20, 0), _
New TimeSpan(0, 45, 10))
ShowTimeSpanSumDiff(outputBlock, _
New TimeSpan(1, 10, 20, 30, 40), _
New TimeSpan(-1, 2, 3, 4, 5))
ShowTimeSpanSumDiff(outputBlock, _
New TimeSpan(182, 12, 30, 30, 505), _
New TimeSpan(182, 11, 29, 29, 495))
ShowTimeSpanSumDiff(outputBlock, _
New TimeSpan(888888888888888), _
New TimeSpan(999999999999999))
End Sub
End Module
' 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
// Example of the TimeSpan.Add( ) and TimeSpan.Subtract( ) methods.
using System;
class Example
{
const string dataFmt = "{0,-24}{1,24}";
// Pad the end of a TimeSpan string with spaces if it does not
// contain milliseconds.
static string Align(TimeSpan interval)
{
string intervalStr = interval.ToString();
int pointIndex = intervalStr.IndexOf(':');
pointIndex = intervalStr.IndexOf('.', pointIndex);
if (pointIndex < 0) intervalStr += " ";
return intervalStr;
}
// Display TimeSpan parameters and their sum and difference.
static void ShowTimeSpanSumDiff(System.Windows.Controls.TextBlock outputBlock, TimeSpan Left, TimeSpan Right)
{
outputBlock.Text += "\n";
outputBlock.Text += String.Format(dataFmt, "TimeSpan Left", Align(Left)) + "\n";
outputBlock.Text += String.Format(dataFmt, "TimeSpan Right", Align(Right)) + "\n";
outputBlock.Text += String.Format(dataFmt, "Left.Add( Right )",
Align(Left.Add(Right))) + "\n";
outputBlock.Text += String.Format(dataFmt, "Left.Subtract( Right )",
Align(Left.Subtract(Right))) + "\n";
}
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
outputBlock.Text +=
"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." + "\n";
// Create pairs of TimeSpan objects.
ShowTimeSpanSumDiff(outputBlock,
new TimeSpan(1, 20, 0),
new TimeSpan(0, 45, 10));
ShowTimeSpanSumDiff(outputBlock,
new TimeSpan(1, 10, 20, 30, 40),
new TimeSpan(-1, 2, 3, 4, 5));
ShowTimeSpanSumDiff(outputBlock,
new TimeSpan(182, 12, 30, 30, 505),
new TimeSpan(182, 11, 29, 29, 495));
ShowTimeSpanSumDiff(outputBlock,
new TimeSpan(888888888888888),
new 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
*/
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Change History
Date |
History |
Reason |
---|---|---|
October 2010 |
Added clarification that this method returns a new TimeSpan object. |
Customer feedback. |