我這樣比較兩個日期:UIDate比較兩個日期
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(@「日期晚於date2」);.爲什麼是這樣?
請注意,該行'NSDate * Date = [NSDate date];'沒有必要(並且沒有ARC泄漏)。做'NSDate * dt1 = [df dateFromString:TheDate];'代替。 – vikingosegundo
好吧,爲了得到當前日期,所以我不需要做NSDate * Date = [NSDate date] – user2014474
對不起,我複製了錯誤的行'NSDate * dt1 = [df dateFromString:TheDate];''而不是' NSDate * dt1 = [[NSDate alloc] init]; dt1 = [df dateFromString:TheDate];' – vikingosegundo