我使用以下代码来比较两个NSDates。结果稍后在transient属性中用于组织表视图部分中的对象。我可以确保核心数据存储中有足够的对象,以便存在三个可能的结果,但NSLog显示选项tmp = @“1”,从不抛出。
-(NSString *)sectionIdentifier{
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp){
NSDate *date = self.todoDueDate;
NSDate *todayDate = [NSDate date];
NSLog(@"date= %@",date);
NSLog(@"todayDate = %@",todayDate);
if([date
compare:todayDate] == NSOrderedSame) {
tmp = @"1";//TODAY
}
else if([date
compare:todayDate] == NSOrderedDescending){
tmp = @"2";//OVERDUE
}
else if ([date
compare:todayDate] == NSOrderedAscending){
tmp =@"0";//UPCOMING
}
NSLog(@"Tmp= %@",tmp);
[self setPrimitiveValue:tmp forKey:@"sectionIdentifier"];
}
这里是日志:
2014-01-15 10:10:52.732 To-Do Pro Light[21458:a0b] date = 2014-01-10 19:53:19 +0000
2014-01-15 10:10:52.735 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.742 To-Do Pro Light[21458:a0b] Tmp= 0
2014-01-15 10:10:52.743 To-Do Pro Light[21458:a0b] date = 2014-01-14 17:45:45 +0000
2014-01-15 10:10:52.746 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.747 To-Do Pro Light[21458:a0b] Tmp= 0
2014-01-15 10:10:52.748 To-Do Pro Light[21458:a0b] date = 2014-01-14 17:57:44 +0000
2014-01-15 10:10:52.749 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.751 To-Do Pro Light[21458:a0b] Tmp= 0
2014-01-15 10:10:52.753 To-Do Pro Light[21458:a0b] date = 2014-01-15 16:31:48 +0000
2014-01-15 10:10:52.755 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.756 To-Do Pro Light[21458:a0b] Tmp= 0
2014-01-15 10:10:52.758 To-Do Pro Light[21458:a0b] date = 2014-01-15 16:32:06 +0000
2014-01-15 10:10:52.759 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.761 To-Do Pro Light[21458:a0b] Tmp= 0
2014-01-15 10:10:52.762 To-Do Pro Light[21458:a0b] date = 2014-01-15 17:03:40 +0000
2014-01-15 10:10:52.763 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.764 To-Do Pro Light[21458:a0b] Tmp= 0
2014-01-15 10:10:52.766 To-Do Pro Light[21458:a0b] date = 2014-01-15 18:33:48 +0000
2014-01-15 10:10:52.767 To-Do Pro Light[21458:a0b] todayDate = 2014-01-15 17:10:52 +0000
2014-01-15 10:10:52.768 To-Do Pro Light[21458:a0b] Tmp= 2
我做错了什么?
答案 0 :(得分:2)
你永远不会得到完全匹配,因为两个NSDate对象必须在当天引用相同的MILLISECOND才能发生。最简单的方法是使用NSDateFormatter将日期格式化为yyyy / MM / dd,然后比较生成的字符串。
(不要忘记根据您的要求适当设置时区。)
答案 1 :(得分:1)
NSDate *startDate = ...;
NSDate *endDate = ...;
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:startDate
toDate:endDate options:0];
NSInteger months = [components month];
NSInteger days = [components day];
NSInteger years = [components year];
如果所有三个都是0 - 日期是相等的。