DateTime.Subtract 方法

定義

傳回會將指定時間或期間從此執行個體的值減去的新 DateTimeReturns a new DateTime that subtracts the specified time or duration from the value of this instance.

多載

Subtract(DateTime)

傳回會將指定日期和時間從此執行個體的值減去的新 DateTimeReturns a new DateTime that subtracts the specified date and time from the value of this instance.

Subtract(TimeSpan)

傳回會將指定期間從此執行個體的值減去的新 DateTimeReturns a new DateTime that subtracts the specified duration from the value of this instance.

Subtract(DateTime)

傳回會將指定日期和時間從此執行個體的值減去的新 DateTimeReturns 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

要減去的日期和時間。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.

例外狀況

這個結果小於 MinValue 或大於 MaxValueThe result is less than MinValue or greater than MaxValue.

範例

下列範例示範 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. 例如,若要從目前的日期減去兩個月,請呼叫值為-2 的 AddMonths(Int32) 方法。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) 方法不會考慮兩個 DateTime 值的 Kind 屬性值。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)

傳回會將指定期間從此執行個體的值減去的新 DateTimeReturns 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

要減去的時間間隔。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.

例外狀況

這個結果小於 MinValue 或大於 MaxValueThe result is less than MinValue or greater than MaxValue.

範例

下列範例示範 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. 例如,若要從目前的日期減去兩個月,請呼叫值為-2 的 AddMonths(Int32) 方法。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.

另請參閱

適用於