DateTimeOffset.Subtract Method

Definition

从当前的 DateTimeOffset 对象中减去指定的时间或持续时间。Subtracts a specified time or duration from the current DateTimeOffset object.

Overloads

Subtract(DateTimeOffset)

从当前的 DateTimeOffset 对象中减去表示特定日期和时间的 DateTimeOffset 值。Subtracts a DateTimeOffset value that represents a specific date and time from the current DateTimeOffset object.

Subtract(TimeSpan)

从当前的 DateTimeOffset 对象中减去指定的时间间隔。Subtracts a specified time interval from the current DateTimeOffset object.

Subtract(DateTimeOffset)

从当前的 DateTimeOffset 对象中减去表示特定日期和时间的 DateTimeOffset 值。Subtracts a DateTimeOffset value that represents a specific date and time from the current DateTimeOffset object.

public:
 TimeSpan Subtract(DateTimeOffset value);
public TimeSpan Subtract (DateTimeOffset value);
member this.Subtract : DateTimeOffset -> TimeSpan
Public Function Subtract (value As DateTimeOffset) As TimeSpan

Parameters

value
DateTimeOffset

一个对象,表示要减去的值。An object that represents the value to subtract.

Returns

TimeSpan

一个对象,指定两个 DateTimeOffset 对象之间的间隔。An object that specifies the interval between the two DateTimeOffset objects.

Examples

下面的示例演示使用 Subtract(DateTimeOffset) 方法的减法运算。The following example illustrates subtraction that uses the Subtract(DateTimeOffset) method.

DateTimeOffset firstDate = new DateTimeOffset(2018, 10, 25, 18, 0, 0,
                                              new TimeSpan(-7, 0, 0));
DateTimeOffset secondDate = new DateTimeOffset(2018, 10, 25, 18, 0, 0,
                                               new TimeSpan(-5, 0, 0));
DateTimeOffset thirdDate = new DateTimeOffset(2018, 9, 28, 9, 0, 0,
                                              new TimeSpan(-7, 0, 0));
TimeSpan difference;

difference = firstDate.Subtract(secondDate);
Console.WriteLine($"({firstDate}) - ({secondDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}");

difference = firstDate.Subtract(thirdDate);
Console.WriteLine($"({firstDate}) - ({thirdDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}");

// The example produces the following output:
//    (10/25/2018 6:00:00 PM -07:00) - (10/25/2018 6:00:00 PM -05:00): 0 days, 2:00
//    (10/25/2018 6:00:00 PM -07:00) - (9/28/2018 9:00:00 AM -07:00): 27 days, 9:00
Dim firstDate As New DateTimeOffset(#10/25/2018 6:00PM#, _
                                    New TimeSpan(-7, 0, 0))
Dim secondDate As New DateTimeOffset(#10/25/2018 6:00PM#, _
                                     New TimeSpan(-5, 0, 0))
Dim thirdDate As New DateTimeOffset(#9/28/2018 9:00AM#, _
                                    New TimeSpan(-7, 0, 0))
Dim difference As TimeSpan

difference = firstDate.Subtract(secondDate)
Console.WriteLine($"({firstDate}) - ({secondDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}")

difference = firstDate.Subtract(thirdDate)
Console.WriteLine($"({firstDate}) - ({thirdDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}")

' The example produces the following output:
'    (10/25/2018 6:00:00 PM -07:00) - (10/25/2018 6:00:00 PM -05:00): 0 days, 2:00
'    (10/25/2018 6:00:00 PM -07:00) - (9/28/2018 9:00:00 AM -07:00): 27 days, 9:00

Remarks

此方法在计算分隔它们的时间间隔之前,将 DateTimeOffset 对象转换为协调世界时(UTC)。This method converts both DateTimeOffset objects to Coordinated Universal Time (UTC) before calculating the time interval that separates them. 这消除了不同于 UTC 的偏移量可能对比较产生的任何影响。This removes any effect that different offsets from UTC may have on the comparison.

Note

对于支持自定义运算符的语言,还可以使用减法运算符来执行日期和时间减法运算。For languages that support custom operators, you can also perform date and time subtraction by using the subtraction operator. 有关详细信息,请参阅 Subtraction 方法。For details, see the Subtraction method.

由于当前 DateTimeOffset 对象和 value 参数都不表示特定时区中的日期和时间,因此,Subtract(DateTimeOffset) 方法在减去日期和时间时,不会考虑特定时区的调整规则。Because neither the current DateTimeOffset object nor the value parameter represent the date and time in a specific time zone, the Subtract(DateTimeOffset) method does not consider a particular time zone's adjustment rules when it subtracts dates and times.

See also

Subtract(TimeSpan)

从当前的 DateTimeOffset 对象中减去指定的时间间隔。Subtracts a specified time interval from the current DateTimeOffset object.

public:
 DateTimeOffset Subtract(TimeSpan value);
public DateTimeOffset Subtract (TimeSpan value);
member this.Subtract : TimeSpan -> DateTimeOffset
Public Function Subtract (value As TimeSpan) As DateTimeOffset

Parameters

value
TimeSpan

待减去的时间间隔。The time interval to subtract.

Returns

DateTimeOffset

一个对象,它等于当前的 DateTimeOffset 对象所表示的日期和时间减去 value 所表示的时间间隔。An object that is equal to the date and time represented by the current DateTimeOffset object, minus the time interval represented by value.

Exceptions

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

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

Examples

下面的示例演示使用 Subtract 方法的减法运算。The following example illustrates subtraction that uses the Subtract method.

DateTimeOffset offsetDate = new DateTimeOffset(2007, 12, 3, 11, 30, 0,
                               new TimeSpan(-8, 0, 0));
TimeSpan duration = new TimeSpan(7, 18, 0, 0);
Console.WriteLine(offsetDate.Subtract(duration).ToString());  // Displays 11/25/2007 5:30:00 PM -08:00
Dim offsetDate As New DateTimeOffset(#12/3/2007 11:30AM#, _
                               New TimeSpan(-8, 0, 0)) 
Dim duration As New TimeSpan(7, 18, 0, 0)
Console.WriteLine(offsetDate.Subtract(duration))    ' Displays 11/25/2007 5:30:00 PM -08:00

Remarks

可以在单个操作中使用 Subtract 方法来减去多个时间间隔(天、小时、分钟、秒或毫秒)。You can use the Subtract method to subtract more than one kind of time interval (days, hours, minutes, seconds, or milliseconds) in a single operation. 它的行为与定义减法运算符的 Subtraction(DateTimeOffset, TimeSpan) 方法相同。Its behavior is identical to the Subtraction(DateTimeOffset, TimeSpan) method, which defines the subtraction operator. DateTimeOffset 结构还支持专用的加法方法(如 AddDaysAddHoursAddMinutes),通过将负值赋给 method 参数来执行减法运算。The DateTimeOffset structure also supports specialized addition methods (such as AddDays, AddHours, and AddMinutes) that allow you to perform subtraction by assigning a negative value to the method parameter.

Note

此方法返回新的 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.

Subtract 方法不会影响 DateTimeOffset 对象的 Offset 属性的值。The Subtract method does not affect the value of the DateTimeOffset object's Offset property. 返回 DateTimeOffset 对象与原始对象具有相同的偏移量。The returned DateTimeOffset object has the same offset as the original object.

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

See also

Applies to