System.DateTime.op_Subtraction Method

Subtracts a specified date and time from another specified date and time and returns a time interval.

Syntax

public static TimeSpan operator- (DateTime d1, DateTime d2)

Parameters

d1
The date and time value to subtract from (the minuend).
d2
The date and time value to subtract (the subtrahend).

Returns

The time interval between d1 and d2; that is, d1 minus d2.

Exceptions

TypeReason
ArgumentOutOfRangeExceptionThe resulting date and time is less than DateTime.MinValue or greater than DateTime.MaxValue.

Remarks

The DateTime.op_Subtraction(DateTime, DateTime) method does not consider the value of the DateTime.Kind property of the two DateTime values when performing the subtraction. 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.

Note:

The DateTimeOffset.op_Subtraction(DateTimeOffset, DateTimeOffset) method does consider the difference between time zones when performing the subtraction.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0