<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.12.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 3.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSDateInterval/compare(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDateInterval(im)compare:"
  },
  "title" : "compare(_:)"
}
-->

# compare(_:)

Compares the receiver with the specified date interval.

```
func compare(_ dateInterval: DateInterval) -> ComparisonResult
```

## Parameters

`dateInterval`

The date interval with which to compare the receiver.

## Return Value

Returns an [`ComparisonResult`](/documentation/Foundation/ComparisonResult) value that indicates the temporal ordering of the receiver and a given date interval:

- [`ComparisonResult.orderedAscending`](/documentation/Foundation/ComparisonResult/orderedAscending) if the receiver’s [`startDate`](/documentation/Foundation/NSDateInterval/startDate) occurs earlier than that of `dateInterval`, or both [`startDate`](/documentation/Foundation/NSDateInterval/startDate) values are equal and the [`duration`](/documentation/Foundation/NSDateInterval/duration) of the receiver is less than that of `dateInterval`.
- [`ComparisonResult.orderedDescending`](/documentation/Foundation/ComparisonResult/orderedDescending) if the receiver’s [`startDate`](/documentation/Foundation/NSDateInterval/startDate) occurs later than that of `dateInterval`, or both [`startDate`](/documentation/Foundation/NSDateInterval/startDate) values are equal and the [`duration`](/documentation/Foundation/NSDateInterval/duration) of the receiver is greater than that of `dateInterval`.
- [`ComparisonResult.orderedSame`](/documentation/Foundation/ComparisonResult/orderedSame) if the receiver’s [`startDate`](/documentation/Foundation/NSDateInterval/startDate) and [`duration`](/documentation/Foundation/NSDateInterval/duration) values are equal to those of `dateInterval`.

## Discussion

The following figure illustrates four `NSDateInterval` objects plotted on an arbitrary time axis. Each date interval spans its [`duration`](/documentation/Foundation/NSDateInterval/duration) from left to right, from its [`startDate`](/documentation/Foundation/NSDateInterval/startDate) to its [`endDate`](/documentation/Foundation/NSDateInterval/endDate).

![](images/com.apple.foundation/media-2556955@2x.png)

The result of comparing the date interval labeled **A** with the date interval labeled **B** is [`ComparisonResult.orderedAscending`](/documentation/Foundation/ComparisonResult/orderedAscending), because **A** has a [`startDate`](/documentation/Foundation/NSDateInterval/startDate) that occurs earlier than that of **B**.

The result of comparing the date interval labeled **C** with the date interval labeled **D** is [`ComparisonResult.orderedDescending`](/documentation/Foundation/ComparisonResult/orderedDescending), because because **C** and **D** have the same [`startDate`](/documentation/Foundation/NSDateInterval/startDate), and **C** has a [`duration`](/documentation/Foundation/NSDateInterval/duration) greater than that of **D**.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)