Compartir a través de


DateTimeOffset.Subtract Method (TimeSpan)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Subtracts a specified time interval from the current DateTimeOffset object.

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

Syntax

'Declaration
Public Function Subtract ( _
    value As TimeSpan _
) As DateTimeOffset
public DateTimeOffset Subtract(
    TimeSpan value
)

Parameters

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.

Exceptions

Exception Condition
ArgumentOutOfRangeException

The resulting DateTimeOffset value is less than MinValue.

-or-

The resulting DateTimeOffset value is greater than MaxValue.

Remarks

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.

Examples

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.