TimeSpan.Add(TimeSpan) Método

Definición

Devuelve un nuevo objeto TimeSpan cuyo valor es la suma del objeto TimeSpan especificado y esta instancia.Returns a new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance.

public:
 TimeSpan Add(TimeSpan ts);
public TimeSpan Add (TimeSpan ts);
member this.Add : TimeSpan -> TimeSpan
Public Function Add (ts As TimeSpan) As TimeSpan

Parámetros

ts
TimeSpan

Intervalo de tiempo que se va a agregar.The time interval to add.

Devoluciones

Objeto nuevo que representa el valor de la instancia en concreto, más el valor de ts.A new object that represents the value of this instance plus the value of ts.

Excepciones

La TimeSpan resultante es menor que MinValue o mayor que MaxValue.The resulting TimeSpan is less than MinValue or greater than MaxValue.

Ejemplos

En el ejemplo siguiente se llama al método Add para agregar cada elemento de una matriz de intervalos de tiempo a un valor de TimeSpan base.The following example calls the Add method to add each element in an array of time intervals to a base TimeSpan value.

Nota

The C# examples in this article run in the Try.NET inline code runner and playground. Select the Run button to run an example in an interactive window. Once you execute the code, you can modify it and run the modified code by selecting Run again. The modified code either runs in the interactive window or, if compilation fails, the interactive window displays all C# compiler error messages.

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
Module Example
   Public Sub Main()
      Dim baseTimeSpan As New TimeSpan(1, 12, 15, 16)
      ' Create an array of timespan intervals.
      Dim intervals() As TimeSpan = { 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.
      For Each 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), If(interval < TimeSpan.Zero, "-", "+"))
      Next                                      
   End Sub
End Module
' 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

Comentarios

El valor devuelto debe estar entre TimeSpan.MinValue y TimeSpan.MaxValue; de lo contrario, se produce una excepción.The return value must be between TimeSpan.MinValue and TimeSpan.MaxValue; otherwise, an exception is thrown.

El valor devuelto es un nuevo TimeSpan; el TimeSpan original no se modifica.The return value is a new TimeSpan; the original TimeSpan is not modified.

Se aplica a

Consulte también: