---
title: "compare(_:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsdateinterval/compare(_:)"
---

# compare(_:)

Compares the receiver with the specified date interval.

## Declaration

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

## Parameters

- `dateInterval`: The date interval with which to compare the receiver.

## Return Value

Return Value Returns an ComparisonResult value that indicates the temporal ordering of the receiver and a given date interval: ComparisonResult.orderedAscending if the receiver’s startDate occurs earlier than that of dateInterval, or both startDate values are equal and the duration of the receiver is less than that of dateInterval. ComparisonResult.orderedDescending if the receiver’s startDate occurs later than that of dateInterval, or both startDate values are equal and the duration of the receiver is greater than that of dateInterval. ComparisonResult.orderedSame if the receiver’s startDate and duration values are equal to those of dateInterval.

## Discussion

Discussion The following figure illustrates four NSDateInterval objects plotted on an arbitrary time axis. Each date interval spans its duration from left to right, from its startDate to its endDate.

The result of comparing the date interval labeled A with the date interval labeled B is ComparisonResult.orderedAscending, because A has a 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, because because C and D have the same startDate, and C has a duration greater than that of D.

## See Also

### Comparing Date Intervals

- [isEqual(to:)](foundation/nsdateinterval/isequal(to:).md)
