NSDate过滤案例

时间:2014-01-18 04:55:49

标签: ios nsdate

我使用transient attributecore data objects中分发table view sectionstransient attribute称为 sectionIdentifier ,它的定义取决于名为 todoDueDate 的属性。 我需要以下列组中的所有必须包含的方式过滤对象并且仅在一个

1. sectionIdentier = 0, **OVERDUE** , todoDueDate < today
2. sectionIdentier = 1, **TODAY** , todoDueDate = today
3. sectionIdentier = 2, **TOMORROW** , todoDueDate >today AND todoDueDate= = tomorrow
4. sectionIdentier = 3, **UPCOMING** , todoDueDate > today AND todoDueDate!=tomorrow

使用我当前的代码,我能够过滤案例1,2和4的对象(我必须在这里丢弃明天的对象)。我已经尝试了几种方法来过滤案例3的对象,但没有成功。 我恳请你帮我过滤案例3。 这是我目前的代码:

-(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);


        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSInteger comps = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);
        NSDateComponents *date1Components = [calendar components:comps fromDate:date];
        NSDateComponents *date2Components = [calendar components:comps fromDate:todayDate];

        date = [calendar dateFromComponents:date1Components];
        todayDate = [calendar dateFromComponents:date2Components];


        if([date
            compare:todayDate] == NSOrderedAscending) {
            tmp = @"0";//OVERDUE
        }
        if([date
          compare:todayDate] == NSOrderedDescending)  {
          tmp = @"3";//UPCOMING BUT NOT TOMORROW(PENDING THIS PART)
        }
        if ([date
             compare:todayDate] == NSOrderedSame) {
            tmp = @"1";//TODAY
        }


   //TOMORROW COMES HERE



        NSLog(@"Tmp= %@",tmp);

        [self setPrimitiveValue:tmp forKey:@"sectionIdentifier"];

    }
    return tmp;

}

这是我更新的代码:

-(NSString *)sectionIdentifier{

    [self willAccessValueForKey:@"sectionIdentifier"];
    NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"];
    [self didAccessValueForKey:@"sectionIdentifier"];

    if (!tmp){
        NSDate *today = [NSDate date];
        NSDate *date = self.todoDueDate;
        NSCalendar *calendar;
        NSInteger daysAfterToday = [calendar components:NSDayCalendarUnit
                                               fromDate:today toDate:date options:0].day;
       // NSString *section;
        if (daysAfterToday < 0) {
            tmp  = @"0";
        } else if (daysAfterToday == 0) {
            tmp = @"1";
        } else if (daysAfterToday == 1) {
            tmp = @"2";
        } else {
            tmp = @"3";
        }


        NSLog(@"TODAY = %@", today);
        NSLog(@"DATE = %@", date);
        NSLog(@"DAYS AFTER TODAY = %ld",(long)daysAfterToday);

        NSLog(@"Tmp= %@",tmp);

        [self setPrimitiveValue:tmp forKey:@"sectionIdentifier"];

    }
    return tmp;

}

这里记录结果:

2014-01-17 22:35:53.576 To-Do Pro Light[4124:a0b] TODAY = 2014-01-18 05:35:53 +0000
2014-01-17 22:35:53.619 To-Do Pro Light[4124:a0b] DATE = 2014-01-13 04:08:32 +0000
2014-01-17 22:35:53.656 To-Do Pro Light[4124:a0b] DAYS AFTER TODAY = 0
2014-01-17 22:35:53.664 To-Do Pro Light[4124:a0b] Tmp= 1
2014-01-17 22:35:53.739 To-Do Pro Light[4124:a0b] TODAY = 2014-01-18 05:35:53 +0000
2014-01-17 22:35:53.743 To-Do Pro Light[4124:a0b] DATE = 2014-01-18 04:13:39 +0000
2014-01-17 22:35:53.748 To-Do Pro Light[4124:a0b] DAYS AFTER TODAY = 0
2014-01-17 22:35:53.752 To-Do Pro Light[4124:a0b] Tmp= 1
2014-01-17 22:35:53.757 To-Do Pro Light[4124:a0b] TODAY = 2014-01-18 05:35:53 +0000
2014-01-17 22:35:53.762 To-Do Pro Light[4124:a0b] DATE = 2014-01-19 04:16:14 +0000
2014-01-17 22:35:53.766 To-Do Pro Light[4124:a0b] DAYS AFTER TODAY = 0
2014-01-17 22:35:53.770 To-Do Pro Light[4124:a0b] Tmp= 1
2014-01-17 22:35:53.776 To-Do Pro Light[4124:a0b] TODAY = 2014-01-18 05:35:53 +0000
2014-01-17 22:35:53.779 To-Do Pro Light[4124:a0b] DATE = 2014-01-25 04:11:34 +0000
2014-01-17 22:35:53.825 To-Do Pro Light[4124:a0b] DAYS AFTER TODAY = 0
2014-01-17 22:35:53.831 To-Do Pro Light[4124:a0b] Tmp= 1

2 个答案:

答案 0 :(得分:1)

稍微改进一下代码,你可以使用消息dateOnly只返回一个日期,没有时间组件(将所有这些代码放在NSDate类别中):

-(NSDate *)dateOnly {
  unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
  NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
  NSDateComponents *components = [calendar components:flags fromDate:self];
  return [calendar dateFromComponents:components];
}

今天会回复你:

+(NSDate *)today {
  return [[NSDate now] dateOnly];
}

然后,鉴于此:

-(NSDate *)addDays:(NSInteger)days {
  NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
  [offsetComponents setDay:days];
  return [[NSDate calendar] dateByAddingComponents:offsetComponents toDate:self options:0];
}

明天你可以:

+(NSDate *)tomorrow {
  return [[NSDate today] addDays:1];
}

通过此,您可以使用[NSDate today][NSDate tomorrow]来简化代码。

另外,请记住从todoDueDate

中删除时间组件
NSDate *date = [self.todoDueDate dateOnly];
NSDate *todayDate = [NSDate today];
NSDate *tomorrowDate = [NSDate tomorrow];

您的逻辑可以简化:

  1. sectionIdentier = 0, OVERDUE ,todoDueDate&lt;今天
  2. sectionIdentier = 1, TODAY ,todoDueDate = today
  3. sectionIdentier = 2, TOMORROW ,todoDueDate =明天
  4. sectionIdentier = 3, UPCOMING ,todoDueDate&gt;明天
  5. 在代码中:

    if ([date compare:todayDate] == NSOrderedAscending) {
      tmp = @"0";//OVERDUE
    }
    else if ([date isEqualToDate:todayDate])  {
      tmp = @"1";//TODAY
    }
    else if ([date isEqualToDate:tomorrowDate])  {
      tmp = @"2";//TOMORROW
    }
    else if ([date compare:tomorrowDate] == NSOrderedDescending)  {
      tmp = @"3";//UPCOMING
    }
    

答案 1 :(得分:1)

只需要求日历计算从今天到截止日期的天数,然后检查该号码:

        NSInteger daysAfterToday = [calendar components:NSDayCalendarUnit
            fromDate:today toDate:date options:0].day;
        NSString *section;
        if (daysAfterToday < 0) {
            section = @"0";
        } else if (daysAfterToday == 0) {
            section = @"1";
        } else if (daysAfterToday == 1) {
            section = @"2";
        } else {
            section = @"3";
        }