0

I am using following code to compare two NSDates. The result is later used in a transient attribute to organise objects in table view sections. I can assure that there are enough objects in the core data store so that the three possible results are present, but the NSLog shows that option tmp = @"1", is never thrown.

-(NSString *)sectionIdentifier{

    [self willAccessValueForKey:@"sectionIdentifier"];
    NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"];
    [self didAccessValueForKey:@"sectionIdentifier"];

    if (!tmp){

        NSDate *date = self.todoDueDate;
        NSDate *todayDate = [NSDate date];

        NSLog(@"date= %@",date);
        NSLog(@"todayDate = %@",todayDate);



        if([date
            compare:todayDate] == NSOrderedSame) {
            tmp = @"1";//TODAY
        }
        else if([date
                 compare:todayDate] == NSOrderedDescending){
            tmp = @"2";//OVERDUE
        }
        else if ([date
                  compare:todayDate] == NSOrderedAscending){
            tmp =@"0";//UPCOMING
        }
        NSLog(@"Tmp= %@",tmp);

        [self setPrimitiveValue:tmp forKey:@"sectionIdentifier"];

    }

Here the log:

2014-01-15 10:10:52.732 To-Do Pro Light[21458:a0b] date      = 2014-01-10 19:53:19 +0000
2014-01-15 10:10:52.735 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.742 To-Do Pro Light[21458:a0b] Tmp= 0
2014-01-15 10:10:52.743 To-Do Pro Light[21458:a0b] date      = 2014-01-14 17:45:45 +0000
2014-01-15 10:10:52.746 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.747 To-Do Pro Light[21458:a0b] Tmp= 0
2014-01-15 10:10:52.748 To-Do Pro Light[21458:a0b] date      = 2014-01-14 17:57:44 +0000
2014-01-15 10:10:52.749 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.751 To-Do Pro Light[21458:a0b] Tmp= 0
2014-01-15 10:10:52.753 To-Do Pro Light[21458:a0b] date      = 2014-01-15 16:31:48 +0000
2014-01-15 10:10:52.755 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.756 To-Do Pro Light[21458:a0b] Tmp= 0
2014-01-15 10:10:52.758 To-Do Pro Light[21458:a0b] date      = 2014-01-15 16:32:06 +0000
2014-01-15 10:10:52.759 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.761 To-Do Pro Light[21458:a0b] Tmp= 0
2014-01-15 10:10:52.762 To-Do Pro Light[21458:a0b] date      = 2014-01-15 17:03:40 +0000
2014-01-15 10:10:52.763 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.764 To-Do Pro Light[21458:a0b] Tmp= 0
2014-01-15 10:10:52.766 To-Do Pro Light[21458:a0b] date      = 2014-01-15 18:33:48 +0000
2014-01-15 10:10:52.767 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.768 To-Do Pro Light[21458:a0b] Tmp= 2

What am I doing wrong?

5
  • 2
    None of the NSDate objects in your log are the same. Why do you expect to see Tmp= 1? Remember, the time (to the millisecond) is included in the comparison.
    – rmaddy
    Jan 15, 2014 at 17:27
  • and how to compare dates without taking into account the time, only year, month and day?
    – mvasco
    Jan 15, 2014 at 17:29
  • @mvasco: NSDateComponents could help here.
    – Larme
    Jan 15, 2014 at 17:31
  • 1
    You're never going to get an exact match, since two NSDate objects would have to refer to the same MILLISECOND in the day for that to occur. Simplest is to use an NSDateFormatter to format the dates to yyyy/MM/dd and compare strings.
    – Hot Licks
    Jan 15, 2014 at 17:34
  • @HotLicks, please put your comment in an answer to let me check it as the selected answer. I will change my code using NSDataFormatter. Thank you.
    – mvasco
    Jan 15, 2014 at 17:42

2 Answers 2

2

You're never going to get an exact match, since two NSDate objects would have to refer to the same MILLISECOND in the day for that to occur. Simplest is to use an NSDateFormatter to format the dates to yyyy/MM/dd and then compare the resulting strings.

(Don't forget to set timezone appropriately for your requirements.)

8
  • Using an NSDateFormatter is far less efficient than using date components.
    – rmaddy
    Jan 15, 2014 at 17:51
  • I will edit my question to add what I have changed, now it is working..thank you..
    – mvasco
    Jan 15, 2014 at 17:52
  • @rmaddy - But much more straight-forward and foolproof. And the efficiency difference is not that great if the formatter is pre-created.
    – Hot Licks
    Jan 15, 2014 at 17:54
  • @rmaddy - The pendulum would swing more towards using datecomponents if they supported Julian day, so that it wasn't necessary to retrieve and combine the three separate numbers.
    – Hot Licks
    Jan 15, 2014 at 17:57
  • Yes that would be a nice addition but keep in mind that using a date formatter most likely uses date components behind the scenes. So now you throw on a bunch of string processing on top of that. That's a lot of wasted processing to save a line or two of code.
    – rmaddy
    Jan 15, 2014 at 18:00
1
NSDate *startDate = ...;
NSDate *endDate = ...;

NSCalendar *gregorian = [[NSCalendar alloc]
                 initWithCalendarIdentifier:NSGregorianCalendar];

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags
                                          fromDate:startDate
                                          toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];
NSInteger years = [components year];

If all three are 0 - the dates are equal.

More at - https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836-SW1

Not the answer you're looking for? Browse other questions tagged or ask your own question.