我想比较两个日期:date1 和 date2。
2011-06-06 12:59:48.994 Project[419:707] firstDate:2011-06-06 10:59:21 +0000
2011-06-06 12:59:49.004 Project[419:707] selectedData:2011-06-06 10:59:17 +0000
但是这些日期具有不同的时间,当我使用NSOrderedSame时它不能正常工作,我该如何解决?
我的代码:
NSDate *firstDate = [[appDelegate.project objectAtIndex:i]objectAtIndex:3];
NSDate *secondDate = [[appDelegate.project objectAtIndex:i]objectAtIndex:4];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger comps = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);
NSDateComponents *date1Components = [calendar components:comps
fromDate:firstDate];
NSDateComponents *date2Components = [calendar components:comps
fromDate:secondDate];
NSDateComponents *date3Components = [calendar components:comps fromDate:appDelegate.selectedDate];
NSLog(@"firstDate:%@", [date1Components date]);
NSLog(@"secondDate:%@", [date2Components date]);
NSLog(@"selectedData:%@", [date3Components date]);
NSComparisonResult compareStart = [[date1Components date] compare: [date3Components date]];
NSComparisonResult compareEnd = [[date2Components date] compare: [date3Components date]];
if ((compareStart == NSOrderedAscending || compareStart == NSOrderedSame)
&& (compareEnd == NSOrderedDescending || compareEnd == NSOrderedSame))
{
NSLog(@"inside");
然后我想在日期1≤选定日期≤日期2时将我的日期和输入进行比较。现在我明白了,因为我收到了一个警告:我应该添加这个“[date1Components date]”,它就可以工作了。问题在于,我在NSLog中有空值,为什么?
NSLog(@"firstDate:%@", [calendar dateFromComponents:date1Components]);
代替NSLog(@"firstDate:%@", [date1Components date]);
。有任何改变吗? - vikingosegundo