DateTimeOffset.Subtract Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Subtracts a specified time or duration from the current DateTimeOffset object.
Subtract(DateTimeOffset) |
Subtracts a DateTimeOffset value that represents a specific date and time from the current DateTimeOffset object. |
Subtract(TimeSpan) |
Subtracts a specified time interval from the current DateTimeOffset object. |
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
Subtracts a DateTimeOffset value that represents a specific date and time from the current DateTimeOffset object.
public:
TimeSpan Subtract(DateTimeOffset value);
public TimeSpan Subtract (DateTimeOffset value);
member this.Subtract : DateTimeOffset -> TimeSpan
Public Function Subtract (value As DateTimeOffset) As TimeSpan
Parameters
- value
- DateTimeOffset
An object that represents the value to subtract.
Returns
An object that specifies the interval between the two DateTimeOffset objects.
Examples
The following example illustrates subtraction that uses the Subtract(DateTimeOffset) method.
DateTimeOffset firstDate = new DateTimeOffset(2018, 10, 25, 18, 0, 0,
new TimeSpan(-7, 0, 0));
DateTimeOffset secondDate = new DateTimeOffset(2018, 10, 25, 18, 0, 0,
new TimeSpan(-5, 0, 0));
DateTimeOffset thirdDate = new DateTimeOffset(2018, 9, 28, 9, 0, 0,
new TimeSpan(-7, 0, 0));
TimeSpan difference;
difference = firstDate.Subtract(secondDate);
Console.WriteLine($"({firstDate}) - ({secondDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}");
difference = firstDate.Subtract(thirdDate);
Console.WriteLine($"({firstDate}) - ({thirdDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}");
// The example produces the following output:
// (10/25/2018 6:00:00 PM -07:00) - (10/25/2018 6:00:00 PM -05:00): 0 days, 2:00
// (10/25/2018 6:00:00 PM -07:00) - (9/28/2018 9:00:00 AM -07:00): 27 days, 9:00
let firstDate = DateTimeOffset(2018, 10, 25, 18, 0, 0, TimeSpan(-7, 0, 0))
let secondDate = DateTimeOffset(2018, 10, 25, 18, 0, 0, TimeSpan(-5, 0, 0))
let thirdDate = DateTimeOffset(2018, 9, 28, 9, 0, 0, TimeSpan(-7, 0, 0))
let difference = firstDate.Subtract secondDate
printfn $"({firstDate}) - ({secondDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}"
let difference = firstDate.Subtract thirdDate
printfn $"({firstDate}) - ({thirdDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}"
// The example produces the following output:
// (10/25/2018 6:00:00 PM -07:00) - (10/25/2018 6:00:00 PM -05:00): 0 days, 2:00
// (10/25/2018 6:00:00 PM -07:00) - (9/28/2018 9:00:00 AM -07:00): 27 days, 9:00
Dim firstDate As New DateTimeOffset(#10/25/2018 6:00PM#, _
New TimeSpan(-7, 0, 0))
Dim secondDate As New DateTimeOffset(#10/25/2018 6:00PM#, _
New TimeSpan(-5, 0, 0))
Dim thirdDate As New DateTimeOffset(#9/28/2018 9:00AM#, _
New TimeSpan(-7, 0, 0))
Dim difference As TimeSpan
difference = firstDate.Subtract(secondDate)
Console.WriteLine($"({firstDate}) - ({secondDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}")
difference = firstDate.Subtract(thirdDate)
Console.WriteLine($"({firstDate}) - ({thirdDate}): {difference.Days} days, {difference.Hours}:{difference.Minutes:d2}")
' The example produces the following output:
' (10/25/2018 6:00:00 PM -07:00) - (10/25/2018 6:00:00 PM -05:00): 0 days, 2:00
' (10/25/2018 6:00:00 PM -07:00) - (9/28/2018 9:00:00 AM -07:00): 27 days, 9:00
Remarks
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.
Note
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.
See also
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
Subtracts a specified time interval from the current DateTimeOffset object.
public:
DateTimeOffset Subtract(TimeSpan value);
public DateTimeOffset Subtract (TimeSpan value);
member this.Subtract : TimeSpan -> DateTimeOffset
Public Function Subtract (value As TimeSpan) As DateTimeOffset
Parameters
- value
- TimeSpan
The time interval to subtract.
Returns
An object that is equal to the date and time represented by the current DateTimeOffset object, minus the time interval represented by value
.
Exceptions
The resulting DateTimeOffset value is less than DateTimeOffset.MinValue.
-or-
The resulting DateTimeOffset value is greater than DateTimeOffset.MaxValue.
Examples
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
let offsetDate = DateTimeOffset(2007, 12, 3, 11, 30, 0, TimeSpan(-8, 0, 0))
let duration = TimeSpan(7, 18, 0, 0)
printfn $"{offsetDate.Subtract duration}" // Displays 11/25/2007 5:30:00 PM -08:00
Dim offsetDate As New DateTimeOffset(#12/3/2007 11:30AM#, _
New TimeSpan(-8, 0, 0))
Dim duration As New TimeSpan(7, 18, 0, 0)
Console.WriteLine(offsetDate.Subtract(duration)) ' Displays 11/25/2007 5:30:00 PM -08:00
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.
Note
This method returns a new DateTimeOffset object. It does not modify the value of the current object by subtracting value
from 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.
See also
Applies to
.NET 9 and other versions
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: