46
if (datStartDate > datEndDate) {

This doesn't seem to work. I know there's a isEqual, etc., but how do I perform "is greater than"?

There are both NSDate.

Jules
  • 7,568
  • 14
  • 102
  • 186
  • Possible duplicate of [How to compare two NSDates: Which is more recent?](https://stackoverflow.com/questions/5965044/how-to-compare-two-nsdates-which-is-more-recent) – Pang Jun 19 '18 at 01:44

7 Answers7

147

The easiest method i'm aware is:

if( [firstDate timeIntervalSinceDate:secondDate] > 0 ) {

The other answers cover compare:, wanted to add some flavour ;).

dannywartnaby
  • 5,512
  • 2
  • 21
  • 11
  • No reason to use this idea because we have -compare: method. But idea is working one for sure. – WINSergey Oct 17 '16 at 17:32
  • A one-liner is big enough reason to use it. +1. – GeneCode Jun 19 '17 at 05:30
  • this does have a an appeal (and flavour) But I think it may return wrong answer for very close NSDates, because of float-calculation and comparison accuracy issues. Better use the 'compare:' or add a more readable semantic wrapper -(BOOL) isEarlierThan: and -(BOOL) isLaterThan: to the existing -(BOOL)isEqual: – Motti Shneor Aug 04 '20 at 07:47
32

To compare dates use -compare: method:

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.
Vladimir
  • 170,431
  • 36
  • 387
  • 313
20

What about...

if ([datStartDate earlierDate: datEndDate] == datStartDate) {
    // datStartDate is earlier
} else {
    // datEndDate is earlier
}
Kevin Mitts
  • 311
  • 1
  • 4
13

As you have NSDates:

NSDate *datStartDate = [NSDate dateWithString:@"2010-10-01 03:00:00 +0900"];
NSDate *datEndDate   = [NSDate dateWithString:@"2010-10-01 04:00:00 +0900"];

if ( ([datStartDate compare:datEndDate]) == NSOrderedDescending ) {
    ...
}
martin clayton
  • 76,436
  • 32
  • 213
  • 198
  • Constructing strings with 'natural language' is now fully deprecated and this will not compile. – lewis Feb 01 '17 at 14:36
2

Swift 2 version of accepted answer:

if firstDate.timeIntervalSinceDate(secondDate) > 0 {
    // firstDate is greater (further in the future) than secondDate
}
brandonscript
  • 68,675
  • 32
  • 163
  • 220
1

I've been working with NSDate and NSComparison result stuff for years and I can never for the life of me remember how this works. So I wrote a convenience extension on Date for it:

func isBefore(_ otherDate: Date) -> Bool {
    let result = self.compare(otherDate)
    switch result {
    case .orderedAscending:
        return true
    case .orderedSame,
         .orderedDescending:
        return false
    }
}

If you want to have an isAfter extension it just has to return true for orderedDescending.

DesignatedNerd
  • 2,514
  • 1
  • 23
  • 46
1
if ([startDate compare:endDate] == NSOrderedAscending) {
        NSLog(@"startDate is EARLIER than endDate");
}
Harshal Valanda
  • 5,331
  • 26
  • 63
joan
  • 2,407
  • 4
  • 29
  • 35