DateTimeOffset.Subtract DateTimeOffset.Subtract DateTimeOffset.Subtract DateTimeOffset.Subtract Method

定义

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

重载

Subtract(DateTimeOffset) Subtract(DateTimeOffset) Subtract(DateTimeOffset) Subtract(DateTimeOffset)

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

Subtract(TimeSpan) Subtract(TimeSpan) Subtract(TimeSpan) Subtract(TimeSpan)

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

Subtract(DateTimeOffset) Subtract(DateTimeOffset) Subtract(DateTimeOffset) 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

参数

value
DateTimeOffset DateTimeOffset DateTimeOffset DateTimeOffset

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

返回

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

示例

下面的示例演示了使用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

注解

此方法将两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.

备注

对于支持自定义运算符的语言,还可以使用减法运算符来执行日期和时间减法运算。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.

另请参阅

Subtract(TimeSpan) Subtract(TimeSpan) Subtract(TimeSpan) 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

参数

value
TimeSpan TimeSpan TimeSpan TimeSpan

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

返回

一个对象,它等于当前的 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.

异常

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

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

示例

下面的示例演示了使用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

注解

在单个操作中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结构还支持专用加法方法( AddDays如、 AddHoursAddMinutes),通过将负值赋给 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.

备注

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

方法不影响DateTimeOffset对象的Offset属性的值。 SubtractThe 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.

另请参阅

适用于