<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.9.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSCalendar/compare(_:to:toUnitGranularity:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSCalendar(im)compareDate:toDate:toUnitGranularity:"
  },
  "title" : "compare(_:to:toUnitGranularity:)"
}
-->

# compare(_:to:toUnitGranularity:)

Indicates the ordering of two given dates based on their components down to a given unit granularity.

```
func compare(_ date1: Date, to date2: Date, toUnitGranularity unit: NSCalendar.Unit) -> ComparisonResult
```

## Parameters

`date1`

The first date to compare.

`date2`

The second date to compare.

`unit`

The smallest unit that must, along with all larger units, be equal for the given dates to be considered the same. For possible values, see [`NSCalendar.Unit`](/documentation/Foundation/NSCalendar/Unit).

## Return Value

`NSOrderedSame` if the dates are the same down to the given granularity, otherwise `NSOrderedAscending` or `NSOrderedDescending`.

## Discussion

---

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)