DateTimeOffset.Add(TimeSpan) 方法

定义

返回一个新的 DateTimeOffset 对象,它向此实例的值添加指定的时间间隔。Returns a new DateTimeOffset object that adds a specified time interval to the value of this instance.

public:
 DateTimeOffset Add(TimeSpan timeSpan);
public DateTimeOffset Add (TimeSpan timeSpan);
member this.Add : TimeSpan -> DateTimeOffset

参数

timeSpan
TimeSpan

一个 TimeSpan 对象,表示正时间间隔或负时间间隔。A TimeSpan object that represents a positive or a negative time interval.

返回

一个对象,其值为当前 DateTimeOffset 对象所表示的日期和时间与 timeSpan 所表示的时间间隔之和。An object whose value is the sum of the date and time represented by the current DateTimeOffset object and the time interval represented by timeSpan.

异常

生成的 DateTimeOffset 值小于 MinValueThe resulting DateTimeOffset value is less than MinValue.

-或--or- 生成的 DateTimeOffset 值大于 MaxValueThe resulting DateTimeOffset value is greater than MaxValue.

示例

下面的示例创建一个 TimeSpan 对象的数组,这些对象表示目标间的航班时间。The following example creates an array of TimeSpan objects that represent the flight times between destinations. 然后 Add 方法将这些时间添加到表示航班初始起飞时间的 DateTimeOffset 对象中。The Add method then adds these times to a DateTimeOffset object that represents a flight's initial takeoff time. 结果反映每个目标的计划的到达时间。The result reflects the scheduled arrival time at each destination.

DateTimeOffset takeOff = new DateTimeOffset(2007, 6, 1, 7, 55, 0, 
                             new TimeSpan(-5, 0, 0));
DateTimeOffset currentTime = takeOff;
TimeSpan[] flightTimes = new TimeSpan[]
                  {new TimeSpan(2, 25, 0), new TimeSpan(1, 48, 0)};
Console.WriteLine("Takeoff is scheduled for {0:d} at {0:T}.", 
                  takeOff);
for (int ctr = flightTimes.GetLowerBound(0); 
     ctr <= flightTimes.GetUpperBound(0); ctr++)
{
   currentTime = currentTime.Add(flightTimes[ctr]);
   Console.WriteLine("Destination #{0} at {1}.", ctr + 1, currentTime);
}
Dim takeOff As New DateTimeOffset(#6/1/2007 7:55AM#, _
                                  New TimeSpan(-5, 0, 0))
Dim currentTime As DateTimeOffset = takeOff
Dim flightTimes() As TimeSpan = New TimeSpan() _
                  {New TimeSpan(2, 25, 0), New TimeSpan(1, 48, 0)}
Console.WriteLine("Takeoff is scheduled for {0:d} at {0:T}.", _
                  takeOff)
For ctr As Integer = flightTimes.GetLowerBound(0) To _
                     flightTimes.GetUpperBound(0)
   currentTime = currentTime.Add(flightTimes(ctr))
   Console.WriteLine("Destination #{0} at {1}.", ctr + 1, currentTime)
Next

注解

可以在单个操作中使用 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 the addition operator. DateTimeOffset 结构还支持每个时间间隔的专用加法方法(如 AddDaysAddHoursAddMinutes)。The DateTimeOffset structure also supports specialized addition methods (such as AddDays, AddHours, and AddMinutes) for each time interval.

备注

此方法返回新的 DateTimeOffset 对象。This method returns a new DateTimeOffset object. 它不会通过将 timeSpan 添加到其日期和时间来修改当前对象的值。It does not modify the value of the current object by adding timeSpan to its date and time.

Add 方法不会影响当前 DateTimeOffset 对象的 Offset 属性的值。The Add method does not affect the value of the current DateTimeOffset object's Offset property.

由于 DateTimeOffset 对象不表示特定时区中的日期和时间,因此 Add 方法在执行日期和时间算术时不会考虑特定时区的调整规则。Because a DateTimeOffset object does not represent the date and time in a specific time zone, the Add method does not consider a particular time zone's adjustment rules when it performs date and time arithmetic.

如果 null``timeSpan 参数,则此方法返回未更改的原始 DateTimeOffset 对象的值。If the timeSpan parameter is null, this method returns the value of the original DateTimeOffset object unchanged.

适用于

另请参阅