3
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSLog([@"today is " stringByAppendingString:[dateFormat stringFromDate:date]]);
NSLog([@"firstBirthdayDate is " stringByAppendingString:[dateFormat stringFromDate:firstBirthdayDate]]);
NSLog([@"secondBirthdayDate is " stringByAppendingString:[dateFormat stringFromDate:secondBirthdayDate]]);
if ([firstBirthdayDate isEqualToDate:secondBirthdayDate])
    NSLog(@"First date is the same as second date");
if (firstBirthdayDate < date)
    NSLog(@"First date is earlier than today");
else
    NSLog(@"First date is later than today");

if (secondBirthdayDate < date)
    NSLog(@"Second date is earlier than today");
  • Today is 11/08/2012
  • firstBirthdayDate is 01/23/2012
  • secondBirthdayDate is 01/23/2012

Here's what I get in the log:

First date is the same as second date

First date is later than today

Second date is earlier than today

I think I'm going crazy...

iDev
  • 23,310
  • 7
  • 60
  • 85
taralex
  • 935
  • 2
  • 12
  • 29
  • `if (firstBirthdayDate < date)` is comparing the value of the pointers to `firstBirthdayDate` and `date` not the date objects stored in those pointers. – mttrb Nov 09 '12 at 04:14
  • But regardless, they are both NSDate objects, how can they be the same, but one of them is later than today and the other is earlier than today? – taralex Nov 09 '12 at 04:15
  • 3
    Although the dates stored in the object are the same, the pointers to the two birthday objects are different so when you compare the pointers (memory addresses) they are not the same. – mttrb Nov 09 '12 at 04:18

4 Answers4

7

Use if ([date1 isEqualToDate:date2]) for comparing two dates or else you can use the following,

if ([date1 compare:date2] == NSOrderedSame)

if ([date1 compare:date2] == NSOrderedAscending)

if ([date1 compare:date2] == NSOrderedDescending)

>, < or = are only for comparing non-pointers. Basically my understanding is that when you are using these operators, it might be comparing the memory addresses rather than the values in it. So you will get unexpected results.

Logically, this is how it works:

    if (obj1 > obj2) {
        return NSOrderedDescending;
    }

    if (obj1 < obj2) {
        return NSOrderedAscending;
    }

    if (obj1 == obj2) {
        return NSOrderedSame;
    }

You can use any of the compare statements to compare dates.

iDev
  • 23,310
  • 7
  • 60
  • 85
  • 1
    It should be `compare:` to compare two dates – JustSid Nov 09 '12 at 04:12
  • 1
    This also answers the question in your question comment. `isEqualToDate` asks "Are they the same date and time?" (equal). `<`, `>` and `==` ask "Are they the same object?" (identical) See this: http://stackoverflow.com/questions/1692863/what-is-the-difference-between-identity-and-equality-in-oop – vicvicvic Nov 09 '12 at 04:20
  • 1
    @taralex Correct, Objective-C doesn't support operator overloading, so like ACB said you are just ending up comparing the pointer of the objects. – JustSid Nov 09 '12 at 04:20
0

You cannot use < or > for comparing dates. You have to use the correct methods. Have a look at this post.

Community
  • 1
  • 1
Nico Huysamen
  • 10,217
  • 9
  • 62
  • 88
0
    if ([date compare:firstBirthdayDate] == NSOrderedAscending){
         NSLog(@"First date is earlier than today");
    }
   else{
        NSLog(@"First date is later than today");
   }
   if ([date compare:secondBirthdayDate] == NSOrderedAscending){
         NSLog(@"Second date is earlier than today");
   }

   if ([firstBirthdayDate compare: secondBirthdayDate] == NSOrderedSame) 
        NSLog(@"First date is the same as second date");
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
0

in short: because basic operators only work on primitive types for any OBJECT < > != == ... does a basic operation on the POINTER value of this variable

in c++ those operators can be overwritten in objC and java and other languages you need to use the isEqual function of NSObject

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135