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.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- value
- Type: System.TimeSpan
The time interval to subtract.
Return Value
Type: System.DateTimeOffsetAn 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.
Note: |
|---|
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 Dim originalTime, localTime As DateTimeOffset originalTime = New DateTimeOffset(#3/11/2007 3:00:00 AM#, _ New TimeSpan(-6, 0, 0)) localTime = originalTime.ToLocalTime() outputBlock.Text += String.Format("Converted {0} to {1}.", originalTime.ToString() & vbCrLf, _ localTime.ToString()) originalTime = New DateTimeOffset(#3/11/2007 4:00:00 AM#, _ New TimeSpan(-6, 0, 0)) localTime = originalTime.ToLocalTime() outputBlock.Text += String.Format("Converted {0} to {1}.", originalTime.ToString() & vbCrLf, _ localTime.ToString()) ' Define a summer UTC time originalTime = New DateTimeOffset(#6/15/2007 8:00:00 AM#, _ TimeSpan.Zero) localTime = originalTime.ToLocalTime() outputBlock.Text += String.Format("Converted {0} to {1}.", originalTime.ToString() & vbCrLf, _ localTime.ToString()) ' Define a winter time originalTime = New DateTimeOffset(#11/30/2007 2:00:00 PM#, _ New TimeSpan(3, 0, 0)) localTime = originalTime.ToLocalTime() outputBlock.Text += String.Format("Converted {0} to {1}.", originalTime.ToString() & vbCrLf, _ localTime.ToString()) ' 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.
Note: