TimeSpan.Add(TimeSpan) 方法

定義

傳回新的 TimeSpan 物件,它的值為指定之 TimeSpan 物件與這個執行個體的總和。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

參數

ts
TimeSpan

要加入的時間間隔。The time interval to add.

傳回

新的物件,表示這個執行個體的值加上 ts 的值。A new object that represents the value of this instance plus the value of ts.

例外狀況

產生的 TimeSpan 小於 MinValue 或大於 MaxValueThe resulting TimeSpan is less than MinValue or greater than MaxValue.

範例

下列範例會呼叫 Add 方法,將時間間隔陣列中的每個元素加入至基底 TimeSpan 值。The following example calls the Add method to add each element in an array of time intervals to a base TimeSpan value.

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

備註

傳回值必須介於 TimeSpan.MinValueTimeSpan.MaxValue之間;否則,就會擲回例外狀況。The return value must be between TimeSpan.MinValue and TimeSpan.MaxValue; otherwise, an exception is thrown.

傳回值是新的 TimeSpan。不會修改原始 TimeSpanThe return value is a new TimeSpan; the original TimeSpan is not modified.

適用於

另請參閱