Skip to content

Instantly share code, notes, and snippets.

@tatimagdalena
Last active June 5, 2023 06:41
Show Gist options
  • Save tatimagdalena/10d45ad5e02d83945475c7826aaee8ec to your computer and use it in GitHub Desktop.
Save tatimagdalena/10d45ad5e02d83945475c7826aaee8ec to your computer and use it in GitHub Desktop.
Compare two dates depending on granularity.
let granularity = Calendar.Component.day
let comparisonResult = Calendar.current.compare(firstDate, to: secondDate, toGranularity: granularity)
// granularity:
// .second
// .minute
// .hour
// .day
// .month
// .year
switch comparisonResult {
case .orderedAscending: print("firstDate is earlier than secondDate, when compared at the specified level of precision")
case .orderedDescending: print("firstDate is later than secondDate, when compared at the specified level of precision")
case .orderedSame: print("firstDate is equal to secondDate, when compared at the specified level of precision")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment