DateTime.Add(TimeSpan) DateTime.Add(TimeSpan) DateTime.Add(TimeSpan) DateTime.Add(TimeSpan) Method

定义

返回一个新的 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 TimeSpan TimeSpan 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.

异常

示例

下面的示例演示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. 结构还支持每个时间间隔的专用加法AddDays方法AddHours(如AddMinutes、和)。 DateTimeThe 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. KindDateTime实例的属性与当前实例的属性相同。The Kind property of the new DateTime instance is the same as that of the current instance.

适用于

另请参阅