---
title: "compare(_:to:toGranularity:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/calendar/compare(_:to:togranularity:)"
---

# compare(_:to:toGranularity:)

Compares two dates down to the specified component.

## Declaration

```swift
func compare(_ date1: Date, to date2: Date, toGranularity component: Calendar.Component) -> ComparisonResult
```

## Parameters

- `date1`: A date to compare.
- `date2`: A date to compare.
- `component`: A granularity to compare. For example, pass .hour to check if two dates are in the same hour.

## Return Value

Return Value .orderedSame if the two dates are equal in the given component and all larger components; otherwise, either .orderedAscending or .orderedDescending.

## See Also

### Comparing Dates

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