---
title: "compare(_:to:toUnitGranularity:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nscalendar/compare(_:to:tounitgranularity:)"
---

# compare(_:to:toUnitGranularity:)

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

## Declaration

```swift
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 doc://com.apple.foundation/documentation/Foundation/NSCalendar/Unit.

## Return Value

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

## See Also

### Comparing Dates

- [isDate(_:equalTo:toUnitGranularity:)](foundation/nscalendar/isdate(_:equalto:tounitgranularity:).md)
- [isDate(_:inSameDayAs:)](foundation/nscalendar/isdate(_:insamedayas:).md)
- [isDateInToday(_:)](foundation/nscalendar/isdateintoday(_:).md)
- [isDateInTomorrow(_:)](foundation/nscalendar/isdateintomorrow(_:).md)
- [isDateInWeekend(_:)](foundation/nscalendar/isdateinweekend(_:).md)
- [isDateInYesterday(_:)](foundation/nscalendar/isdateinyesterday(_:).md)
