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.

适用于

另请参阅