我想比較兩個日期,但我看不到它是如何工作在所有...目標C,比較兩個日期
這是我使用的代碼:
NSCalendar* myCalendar = [NSCalendar currentCalendar];
NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:[NSDate date]];
[components setYear:2012];
[components setMonth:03];
[components setDay:25];
NSDate *startDate = [myCalendar dateFromComponents:components];
NSDateComponents* components2 = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:[NSDate date]];
[components2 setYear:2012];
[components2 setMonth:04];
[components2 setDay:25];
NSDate *endDate = [myCalendar dateFromComponents:components];
NSComparisonResult result1 = [now compare:startDate];
NSComparisonResult result2 = [now compare:endDate];
if (result2 &&
result1
) {
int x =1;
}
的即使這兩個日期在未來,我仍然可以進入if條件......任何原因?