如何根据TAPKU日历中的日期检查NSDate

时间:2011-10-26 05:05:55

标签: ios calendar nsdate tapku

我正在使用核心数据来存储我的项目的对象。对象中有一个日期字段。我想检查用于iphone的Tapku日历组件的点击日期。我的对象上的日期格式为

2011-01-10,这是一个本地化的日期。

如何根据tapku日历控件返回的日期检查日期。

2 个答案:

答案 0 :(得分:2)

是的,你可以,这就是我做的工作:

关于方法:(NSArray *)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate

  • 使正常的核心数据获取以获取对象数组
  • 使用[NSArray indexesOfObjectsPassingTest:]方法开始每天从startDate检查到lastDate
  • 对于所有通过测试添加和对象到标记数组的所有人,并添加一个字典,其中测试日为关键,而获取的对象为值

警告:确保将NSDate对象转换为无时间和GMT:0日期,以使日期比较工作(以艰难的方式学习......)

这是一些代码......

self.marksArray = [NSMutableArray array];
self.dataDictionary = [NSMutableDictionary dictionary];

NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDateComponents *comp = [cal components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit) fromDate:start];

NSDate *ds = [cal dateFromComponents:comp];

// Init offset components to increment days in the loop by one each time
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];


while (YES) {
    // Is the date beyond the last date? If so, exit the loop.
    // NSOrderedDescending = the left value is greater than the right
    if ([ds compare:end] == NSOrderedDescending) {
        break;
    }

    NSIndexSet *indexes = [arrayWithObjectsFromCoreData indexesOfObjectsPassingTest:
                           ^BOOL (id el, NSUInteger i, BOOL *stop) {
                               NSDate *upd = [(YOUR_ENTINY *)el updated];

                               NSDate *day=[self midnightUTC:upd]; 
                               if([ds isSameDay:day]){ 
                                   return TRUE;
                               }
                               return FALSE;
                           }];



    if (indexes.count>0) {

        [self.marksArray addObject:[NSNumber numberWithBool:YES]];
        [self.dataDictionary setObject:[wods objectsAtIndexes:indexes] forKey:ds];


    }else {
        //no wods in this date
        [self.dataArray addObject:[NSNumber numberWithBool:NO]];
    } 

    // Increment day using offset components (ie, 1 day in this instance)
    ds = [cal dateByAddingComponents:offsetComponents toDate:ds options:0];



}
[offsetComponents release];

midnightUTC方法可以在这里找到: http://www.voidasterisk.org/post/4209842795/how-to-calculate-nsdate-of-midnight-in-ios

答案 1 :(得分:0)

可以处理更多信息但是tapku库在NSDate上有一个名为isSameDay的扩展方法,所以你可以使用它:

if ([myDate isSameDay:returnedDate])
    // Do something here

这就是你要找的东西吗?

相关问题