I have two dates: how do I compare which one is greater date in Objective-C?
-
Does this answer your question? [iOS: Compare two dates](https://stackoverflow.com/questions/6112075/ios-compare-two-dates) – Cœur Apr 20 '21 at 13:10
1 Answers
From NSDate class reference:
NSDate -compare: Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.
- (NSComparisonResult)compare:(NSDate *)anotherDate
Parameters anotherDate The date with which to compare the receiver.
This value must not be nil. If the value is nil, the behavior is undefined and may change in future versions of Mac OS X.
Return Value If:
The receiver and anotherDate are exactly equal to each other, NSOrderedSame
The receiver is later in time than anotherDate, NSOrderedDescending
The receiver is earlier in time than anotherDate, NSOrderedAscending.
Discussion This method detects sub-second differences between dates. If you want to compare dates with a less fine granularity, use timeIntervalSinceDate: to compare the two dates.

- 170,431
- 36
- 387
- 313
-
You'll need to use NSDateFormat to convert your strings to dates. – Epsilon Prime Nov 07 '09 at 16:16
-
4There's also -[NSDate earlierDate:] and -[NSDate laterDate:], which are more appropriate if what you really care about is finding the latest of two dates. – Lily Ballard Nov 08 '09 at 08:20