Subtract Method (TimeSpan)
Collapse the table of content
Expand the table of content

DateTimeOffset.Subtract Method (TimeSpan)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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.

ExceptionCondition
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.

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.


// Local time changes on 3/11/2007 at 2:00 AM
DateTimeOffset originalTime, localTime;

originalTime = new DateTimeOffset(2007, 3, 11, 3, 0, 0,
                                  new TimeSpan(-6, 0, 0));
localTime = originalTime.ToLocalTime();
outputBlock.Text += String.Format("Converted {0} to {1}.", originalTime.ToString(),
                                           localTime.ToString()) + "\n";

originalTime = new DateTimeOffset(2007, 3, 11, 4, 0, 0,
                                  new TimeSpan(-6, 0, 0));
localTime = originalTime.ToLocalTime();
outputBlock.Text += String.Format("Converted {0} to {1}.", originalTime.ToString(),
                                           localTime.ToString()) + "\n";

// Define a summer UTC time
originalTime = new DateTimeOffset(2007, 6, 15, 8, 0, 0,
                                  TimeSpan.Zero);
localTime = originalTime.ToLocalTime();
outputBlock.Text += String.Format("Converted {0} to {1}.", originalTime.ToString(),
                                           localTime.ToString()) + "\n";

// Define a winter time
originalTime = new DateTimeOffset(2007, 11, 30, 14, 0, 0,
                                  new TimeSpan(3, 0, 0));
localTime = originalTime.ToLocalTime();
outputBlock.Text += String.Format("Converted {0} to {1}.", originalTime.ToString(),
                                           localTime.ToString()) + "\n";
// The example produces the following output:
//    Converted 3/11/2007 3:00:00 AM -06:00 to 3/11/2007 1:00:00 AM -08:00.
//    Converted 3/11/2007 4:00:00 AM -06:00 to 3/11/2007 3:00:00 AM -07:00.
//    Converted 6/15/2007 8:00:00 AM +00:00 to 6/15/2007 1:00:00 AM -07:00.
//    Converted 11/30/2007 2:00:00 PM +03:00 to 11/30/2007 3:00:00 AM -08:00.                                                           


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft