I’m perplexed as I’m not the method below is seemingly NOT working when looking at my application when both dates are the same Year/Month/Date/Hour/Minute/Sec, HOWEVER when I write a unit test to test it it seems to work fine. Is the code below not robust for some reason?
Code is:
- (BOOL)isAfterThisDate:(NSDate*)thisDate {
NSComparisonResult result = [self compare:thisDate];
if (result == NSOrderedDescending) {
NSLog(@" - isAfterThisDate: %@ is after %@ is TRUE", [self stringSummary], [thisDate stringSummary]);
return TRUE;
} else {
NSLog(@" - isAfterThisDate: %@ is after %@ is FALSE", [self stringSummary], [thisDate stringSummary]);
return FALSE;
}
}
Here’s an extract from my application log (i.e. when I use this method in my application). I was expecting this to be false like my unit test supports.
isAfterThisDate: Thu 03-11-2011 09:00:00 GMT+10:00 is after Thu 03-11-2011 09:00:00 GMT+10:00 is TRUE
For reference the helper method I used is:
- (NSString*) stringSummary {
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"EEE dd-MM-yyyy HH:mm:ss zzz"];
NSString* str = [formatter stringFromDate:self];
return str;
}
It’s generally better to use
So in this case, -earlierDate: or -laterDate: , if you’re just going to use the resultant date object itself, or if you’re just testing a given date, then yes, -timeIntervalSinceLastDate.
Generally speaking, formatters are only for generating human-readable forms of a value and not for doing operations on the values themselves.