DateTime.Add(TimeSpan) 方法

定義

傳回新的 DateTime,將這個執行個體的值加上指定之 TimeSpan 的值。Returns a new DateTime that adds the value of the specified TimeSpan to the value of this instance.

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

參數

value
TimeSpan

正數或負數時間間隔。A positive or negative time interval.

傳回

物件,其值為這個執行個體所表示日期和時間加上 value 所表示時間間隔的總和。An object whose value is the sum of the date and time represented by this instance and the time interval represented by value.

例外狀況

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

範例

下列範例示範 Add 方法。The following example demonstrates the Add method. 它會計算從今天起算36天(864小時)的星期幾。It calculates the day of the week that is 36 days (864 hours) from this moment.

// Calculate what day of the week is 36 days from this instant.
System::DateTime today = System::DateTime::Now;
System::TimeSpan duration( 36, 0, 0, 0 );
System::DateTime answer = today.Add( duration );
System::Console::WriteLine( " {0:dddd}", answer );
// Calculate what day of the week is 36 days from this instant.
System.DateTime today = System.DateTime.Now;
System.TimeSpan duration = new System.TimeSpan(36, 0, 0, 0);
System.DateTime answer = today.Add(duration);
System.Console.WriteLine("{0:dddd}", answer);
' Calculate what day of the week is 36 days from this instant.
Dim today As System.DateTime
Dim duration As System.TimeSpan
Dim answer As System.DateTime

today = System.DateTime.Now
duration = New System.TimeSpan(36, 0, 0, 0)
answer = today.Add(duration)

System.Console.WriteLine("{0:dddd}", answer)

備註

您可以使用 Add 方法,在單一作業中新增一種以上的時間間隔(天、小時、分鐘、秒或毫秒)。You can use the Add method to add more than one kind of time interval (days, hours, minutes, seconds, or milliseconds) in a single operation. 這個方法的行為與加法運算子的行為相同。This method's behavior is identical to that of the addition operator. DateTime 結構也支援每個時間間隔的特殊加法方法(例如 AddDaysAddHoursAddMinutes)。The DateTime structure also supports specialized addition methods (such as AddDays, AddHours, and AddMinutes) for each time interval.

Add 方法會將閏年與執行日期算術時的月份天數納入考慮。The Add method takes into account leap years and the number of days in a month when performing date arithmetic.

這個方法不會變更此 DateTime的值。This method does not change the value of this DateTime. 相反地,它會傳回新的 DateTime,其值為這項作業的結果。Instead, it returns a new DateTime whose value is the result of this operation. DateTime 實例的 Kind 屬性與目前實例相同。The Kind property of the new DateTime instance is the same as that of the current instance.

適用於

另請參閱