我以这种方式比较两个日期:
NSDateFormatter *df= [[NSDateFormatter alloc] init];
[df setDateFormat:@" dd : MM : yyyy"];
NSDate *dt1 = [[NSDate alloc] init];
dt1=[df dateFromString:TheDate];
NSDate *Date=[NSDate date];
NSLog (@"DATE CURRENT: %@ DATE 2 %@", Date, dt1);
if ([Date compare:dt1] == NSOrderedDescending) {
NSLog(@"Date is later than date2");
} else if ([Date compare:dt1] == NSOrderedAscending) {
NSLog(@"Date is earlier than date2");
[postsToBeDeleted addObject: textmessage];
} else {
NSLog(@"dates are the same");
}
NSLog 给了我:
DATE CURRENT: 2013-02-05 21:37:54 +0000 DATE 2 2012-01-04 23:00:00 +0000
但是很明显,当前日期远远早于 dt1 日期,但我仍然得到 NSLog(@"Date is later than date2");。为什么是这样?