0

I have 2 dates in the following format:

August 6, 2012

I am using the following code to attempt to determine which date is later:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MMMM d, yyyy"];
        NSString *today = [dateFormatter stringFromDate:[NSDate date]];
        NSLog(@"%@", today);
        NSDate *todayDate = [dateFormatter dateFromString:today];

        NSLog(@"TodayDate: %@", todayDate);
        for (Job *j in appDelegate.jobs){
            if ([j.dueDate laterDate:todayDate]){
                [jobsToDisplay addObject:j];
            }
        }

However this code does not work, it seems to return dates that are a day + 1 hour behind what they should be. Can anyone explain what I need to do here?

Thanks a lot,

Tysin

Jack Nutkins
  • 1,555
  • 5
  • 36
  • 71
  • Have you looked at [this one](http://stackoverflow.com/questions/1072848/how-to-check-if-an-nsdate-occurs-between-two-other-nsdates)? It might help you out! – Paul Peelen Aug 13 '12 at 08:00

2 Answers2

1

You should set the time zone to UTC (or whatever you are using) to get exact date.

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

Use the below code to compare dates

[NSDate compare:obj]
1

You can use compare: instance method of NSDate

//If j.dueDate = today
if ([j.dueDate compare:todayDate] == NSOrderedSame) {
        return YES;
    }
//j.dueDate is latest
    else if ([j.dueDate compare:todayDate] == NSOrderedDescending) {
        return YES;
    }

For more Detail https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html#//apple_ref/doc/c_ref/NSComparisonResult

Ab'initio
  • 5,368
  • 4
  • 28
  • 40