Last active
June 5, 2023 06:41
-
-
Save tatimagdalena/10d45ad5e02d83945475c7826aaee8ec to your computer and use it in GitHub Desktop.
Compare two dates depending on granularity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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