DateTimeOffset.Subtract Method (TimeSpan)

 

Subtracts a specified time interval from the current DateTimeOffset object.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public DateTimeOffset Subtract(
	TimeSpan value
)

Parameters

value
Type: System.TimeSpan

The time interval to subtract.

Return Value

Type: System.DateTimeOffset

An object that is equal to the date and time represented by the current DateTimeOffset object, minus the time interval represented by value.

Exception Condition
ArgumentOutOfRangeException

The resulting DateTimeOffset value is less than MinValue.

-or-

The resulting DateTimeOffset value is greater than MaxValue.

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. Its behavior is identical to the Subtraction(DateTimeOffset, TimeSpan) method, which defines the subtraction operator. 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.

System_CAPS_noteNote

This method returns a new DateTimeOffset object. It does not modify the value of the current object by adding timeSpan to its date and time.

The Subtract method does not affect the value of the DateTimeOffset object's Offset property. The returned DateTimeOffset object has the same offset as the original object.

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.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: