DateTimeOffset.Subtract Method (DateTimeOffset)

 

Subtracts a DateTimeOffset value that represents a specific date and time from the current DateTimeOffset object.

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

public TimeSpan Subtract(
	DateTimeOffset value
)

Parameters

value
Type: System.DateTimeOffset

An object that represents the value to subtract.

Return Value

Type: System.TimeSpan

An object that specifies the interval between the two DateTimeOffset objects.

This method converts both DateTimeOffset objects to Coordinated Universal Time (UTC) before calculating the time interval that separates them. This removes any effect that different offsets from UTC may have on the comparison.

System_CAPS_noteNote

For languages that support custom operators, you can also perform date and time subtraction by using the subtraction operator. For details, see the Subtraction method.

Because neither the current DateTimeOffset object nor the value parameter represent the date and time in a specific time zone, the Subtract(DateTimeOffset) method does not consider a particular time zone's adjustment rules when it subtracts dates and times.

The following example illustrates subtraction that uses the Subtract(DateTimeOffset) method.

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.Subtract(secondDate);
Console.WriteLine("({0}) - ({1}): {2} days, {3}:{4:d2}", 
                  firstDate.ToString(), 
                  secondDate.ToString(), 
                  difference.Days, 
                  difference.Hours, 
                  difference.Minutes);      

difference = firstDate.Subtract(thirdDate);
Console.WriteLine("({0}) - ({1}): {2} days, {3}:{4:d2}", 
                  firstDate.ToString(), 
                  secondDate.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) - (3/25/2008 6:00:00 PM -05:00): 26 days, 9: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: