DateTime.Subtract DateTime.Subtract DateTime.Subtract DateTime.Subtract Method

定义

返回一个新的 DateTime,从此实例的值中减去指定的时间或持续时间。Returns a new DateTime that subtracts the specified time or duration from the value of this instance.

重载

Subtract(DateTime) Subtract(DateTime) Subtract(DateTime) Subtract(DateTime)

返回一个新的 DateTime,从此实例的值中减去指定的日期和时间。Returns a new DateTime that subtracts the specified date and time from the value of this instance.

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

返回一个新的 DateTime,从此实例的值中减去指定持续时间。Returns a new DateTime that subtracts the specified duration from the value of this instance.

Subtract(DateTime) Subtract(DateTime) Subtract(DateTime) Subtract(DateTime)

返回一个新的 DateTime,从此实例的值中减去指定的日期和时间。Returns a new DateTime that subtracts the specified date and time from the value of this instance.

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

参数

value
DateTime DateTime DateTime DateTime

要减去的日期和时间值。The date and time value to subtract.

返回

一个时间间隔,它等于此实例所表示的日期和时间减去 value 所表示的日期和时间。A time interval that is equal to the date and time represented by this instance minus the date and time represented by value.

异常

示例

下面的示例演示Subtract方法和减法运算符。The following example demonstrates the Subtract method and the subtraction operator.

System::DateTime date1 = System::DateTime( 1996, 6, 3, 22, 15, 0 );
System::DateTime date2 = System::DateTime( 1996, 12, 6, 13, 2, 0 );
System::DateTime date3 = System::DateTime( 1996, 10, 12, 8, 42, 0 );

// diff1 gets 185 days, 14 hours, and 47 minutes.
System::TimeSpan diff1 = date2.Subtract( date1 );

// date4 gets 4/9/1996 5:55:00 PM.
System::DateTime date4 = date3.Subtract( diff1 );

// diff2 gets 55 days 4 hours and 20 minutes.
System::TimeSpan diff2 = date2 - date3;

// date5 gets 4/9/1996 5:55:00 PM.
System::DateTime date5 = date1 - diff2;
System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);

// diff1 gets 185 days, 14 hours, and 47 minutes.
System.TimeSpan diff1 = date2.Subtract(date1);

// date4 gets 4/9/1996 5:55:00 PM.
System.DateTime date4 = date3.Subtract(diff1);

// diff2 gets 55 days 4 hours and 20 minutes.
System.TimeSpan diff2 = date2 - date3;

// date5 gets 4/9/1996 5:55:00 PM.
System.DateTime date5 = date1 - diff2;
Dim date1 As New System.DateTime(1996, 6, 3, 22, 15, 0)
Dim date2 As New System.DateTime(1996, 12, 6, 13, 2, 0)
Dim date3 As New System.DateTime(1996, 10, 12, 8, 42, 0)

Dim diff1 As System.TimeSpan
' diff1 gets 185 days, 14 hours, and 47 minutes.
diff1 = date2.Subtract(date1)

Dim date4 As System.DateTime
' date4 gets 4/9/1996 5:55:00 PM.
date4 = date3.Subtract(diff1)

Dim diff2 As System.TimeSpan
' diff2 gets 55 days 4 hours and 20 minutes.
diff2 = System.DateTime.op_Subtraction(date2, date3)

Dim date5 As System.DateTime
' date5 gets 4/9/1996 5:55:00 PM.
date5 = System.DateTime.op_Subtraction(date1, diff2)

注解

Subtract(DateTime)方法确定两个日期之间的差异。The Subtract(DateTime) method determines the difference between two dates. 若要从当前实例中减去时间间隔, 请调用Subtract(TimeSpan)方法。To subtract a time interval from the current instance, call the Subtract(TimeSpan) method. 若要从当前实例中减去特定时间间隔, 请调用将该时间间隔添加到当前日期的方法, 并提供一个负值作为方法自变量。To subtract a particular time interval from the current instance, call the method that adds that time interval to the current date, and supply a negative value as the method argument. 例如, 若要从当前日期中减去两个月, 请AddMonths(Int32)使用值-2 调用方法。For example, to subtract two months from the current date, call the AddMonths(Int32) method with a value of -2.

如果当前实例的日期和时间早于value, 则该方法返回一个TimeSpan表示负时间跨度的对象。If the date and time of the current instance is earlier than value, the method returns a TimeSpan object that represents a negative time span. 也就是说, 其所有非零属性 (如DaysTicks) 的值均为负数。That is, the value of all of its non-zero properties (such as Days or Ticks) is negative.

执行Subtract(DateTime)减法运算时, 此方法不会Kind考虑两个DateTime值的属性的值。The Subtract(DateTime) method does not consider the value of the Kind property of the two DateTime values when performing the subtraction. 在减去DateTime对象之前, 请确保对象表示相同时区中的时间。Before subtracting DateTime objects, ensure that the objects represent times in the same time zone. 否则, 结果将包括时区之间的差异。Otherwise, the result will include the difference between time zones.

备注

DateTimeOffset.Subtract(DateTimeOffset)执行减法运算时, 此方法会考虑时区之间的差异。The DateTimeOffset.Subtract(DateTimeOffset) method does consider the difference between time zones when performing the subtraction.

另请参阅

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

返回一个新的 DateTime,从此实例的值中减去指定持续时间。Returns a new DateTime that subtracts the specified duration from the value of this instance.

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

参数

value
TimeSpan TimeSpan TimeSpan TimeSpan

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

返回

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

异常

示例

下面的示例演示Subtract方法和减法运算符。The following example demonstrates the Subtract method and the subtraction operator.

System::DateTime date1 = System::DateTime( 1996, 6, 3, 22, 15, 0 );
System::DateTime date2 = System::DateTime( 1996, 12, 6, 13, 2, 0 );
System::DateTime date3 = System::DateTime( 1996, 10, 12, 8, 42, 0 );

// diff1 gets 185 days, 14 hours, and 47 minutes.
System::TimeSpan diff1 = date2.Subtract( date1 );

// date4 gets 4/9/1996 5:55:00 PM.
System::DateTime date4 = date3.Subtract( diff1 );

// diff2 gets 55 days 4 hours and 20 minutes.
System::TimeSpan diff2 = date2 - date3;

// date5 gets 4/9/1996 5:55:00 PM.
System::DateTime date5 = date1 - diff2;
System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);

// diff1 gets 185 days, 14 hours, and 47 minutes.
System.TimeSpan diff1 = date2.Subtract(date1);

// date4 gets 4/9/1996 5:55:00 PM.
System.DateTime date4 = date3.Subtract(diff1);

// diff2 gets 55 days 4 hours and 20 minutes.
System.TimeSpan diff2 = date2 - date3;

// date5 gets 4/9/1996 5:55:00 PM.
System.DateTime date5 = date1 - diff2;
Dim date1 As New System.DateTime(1996, 6, 3, 22, 15, 0)
Dim date2 As New System.DateTime(1996, 12, 6, 13, 2, 0)
Dim date3 As New System.DateTime(1996, 10, 12, 8, 42, 0)

Dim diff1 As System.TimeSpan
' diff1 gets 185 days, 14 hours, and 47 minutes.
diff1 = date2.Subtract(date1)

Dim date4 As System.DateTime
' date4 gets 4/9/1996 5:55:00 PM.
date4 = date3.Subtract(diff1)

Dim diff2 As System.TimeSpan
' diff2 gets 55 days 4 hours and 20 minutes.
diff2 = System.DateTime.op_Subtraction(date2, date3)

Dim date5 As System.DateTime
' date5 gets 4/9/1996 5:55:00 PM.
date5 = System.DateTime.op_Subtraction(date1, diff2)

注解

Subtract(TimeSpan)方法返回与当前实例的指定时间间隔的日期。The Subtract(TimeSpan) method returns the date that is a specified time interval difference from the current instance. 若要确定两个日期之间的时间间隔, Subtract(DateTime)请调用方法。To determine the time interval between two dates, call the Subtract(DateTime) method. 若要从当前实例中减去特定时间间隔, 请调用将该时间间隔添加到当前日期的方法, 并提供一个负值作为方法自变量。To subtract a particular time interval from the current instance, call the method that adds that time interval to the current date, and supply a negative value as the method argument. 例如, 若要从当前日期中减去两个月, 请AddMonths(Int32)使用值-2 调用方法。For example, to subtract two months from the current date, call the AddMonths(Int32) method with a value of -2.

此方法不更改此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.Subtract(TimeSpan)方法会TimeSpan减去表示正时间跨度的对象, 并返回一个DateTime早于当前实例的日期和时间的值。Ordinarily, the DateTime.Subtract(TimeSpan) method subtracts a TimeSpan object that represents a positive time span and returns a DateTime value that is earlier than the date and time of the current instance. 但是, 如果TimeSpan对象表示负时间跨度, 则该DateTime.Subtract(TimeSpan)方法将返回一个DateTime值, 该值晚于当前实例的日期和时间。However, if the TimeSpan object represents a negative time span, the DateTime.Subtract(TimeSpan) method returns a DateTime value that is later than the date and time of the current instance.

使用DateTime.Subtract(TimeSpan)方法可以减去包含多个时间单位的时间间隔 (例如, 给定小时数和给定分钟数)。The DateTime.Subtract(TimeSpan) method allows you to subtract a time interval that consists of more than one unit of time (such as a given number of hours and a given number of minutes). 若要从DateTime实例中减去单个时间单位 (例如年、月或日), 可以将负值数值作为参数传递给以下任何方法:To subtract a single unit of time (such as years, months, or days) from the DateTime instance, you can pass a negative numeric value as a parameter to any of the following methods:

  • AddYears, 用于从当前日期和时间实例中减去特定的年数。AddYears, to subtract a specific number of years from the current date and time instance.

  • AddMonths, 用于从当前日期和时间实例中减去特定的月份数。AddMonths, to subtract a specific number of months from the current date and time instance.

  • AddDays, 用于从当前日期和时间实例减去特定天数。AddDays, to subtract a specific number of days from the current date and time instance.

  • AddHours, 用于从当前日期和时间实例中减去特定的小时数。AddHours, to subtract a specific number of hours from the current date and time instance.

  • AddMinutes, 用于从当前日期和时间实例减去特定分钟数。AddMinutes, to subtract a specific number of minutes from the current date and time instance.

  • AddSeconds, 用于从当前日期和时间实例中减去特定的秒数。AddSeconds, to subtract a specific number of seconds from the current date and time instance.

  • AddMilliseconds, 用于从当前日期和时间实例减去特定的毫秒数。AddMilliseconds, to subtract a specific number of milliseconds from the current date and time instance.

  • AddTicks, 用于从当前日期和时间实例减去特定的计时周期数。AddTicks, to subtract a specific number of ticks from the current date and time instance.

另请参阅

适用于