DateTimeOffset.Subtraction Operator

Definition

Subtracts a specified DateTimeOffset or TimeSpan object from a DateTimeOffset object.

Overloads

Subtraction(DateTimeOffset, DateTimeOffset)

Subtracts one DateTimeOffset object from another and yields a time interval.

Subtraction(DateTimeOffset, TimeSpan)

Subtracts a specified time interval from a specified date and time, and yields a new date and time.

Subtraction(DateTimeOffset, DateTimeOffset)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

Subtracts one DateTimeOffset object from another and yields a time interval.

public static TimeSpan operator - (DateTimeOffset left, DateTimeOffset right);

Parameters

left
DateTimeOffset

The minuend.

right
DateTimeOffset

The subtrahend.

Returns

An object that represents the difference between left and right.

Remarks

The Subtraction method defines the subtraction operation for DateTimeOffset objects. It enables code such as the following:

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

difference = firstDate - secondDate;
Console.WriteLine("({0}) - ({1}): {2} days, {3}:{4:d2}",
                  firstDate.ToString(),
                  secondDate.ToString(),
                  difference.Days,
                  difference.Hours,
                  difference.Minutes);

difference = firstDate - thirdDate;
Console.WriteLine("({0}) - ({1}): {2} days, {3}:{4:d2}",
                  firstDate.ToString(),
                  thirdDate.ToString(),
                  difference.Days,
                  difference.Hours,
                  difference.Minutes);
// The example produces the following output:
//    (3/25/2008 6:00:00 PM -07:00) - (3/25/2008 6:00:00 PM -05:00): 0 days, 2:00
//    (3/25/2008 6:00:00 PM -07:00) - (2/28/2008 9:00:00 AM -07:00): 26 days, 9:00

Languages that do not support custom operators and operator overloading can call the DateTimeOffset.Subtract(DateTimeOffset) method instead.

The equivalent method for this operator is DateTimeOffset.Subtract(TimeSpan).

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Subtraction(DateTimeOffset, TimeSpan)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

Subtracts a specified time interval from a specified date and time, and yields a new date and time.

public static DateTimeOffset operator - (DateTimeOffset dateTimeOffset, TimeSpan timeSpan);
public static DateTimeOffset operator - (DateTimeOffset dateTimeTz, TimeSpan timeSpan);

Parameters

dateTimeOffsetdateTimeTz
DateTimeOffset

The date and time object to subtract from.

timeSpan
TimeSpan

The time interval to subtract.

Returns

An object that is equal to the value of dateTimeOffset minus timeSpan.

Exceptions

Remarks

The Subtraction method defines the subtraction operation for DateTimeOffset objects. It enables code such as the following:

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 - duration);  // Displays 11/25/2007 5:30:00 PM -08:00

Languages that do not support custom operators and operator overloading can call the DateTimeOffset.Subtract(TimeSpan) method instead.

The equivalent method for this operator is DateTimeOffset.Subtract(TimeSpan).

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0