首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据TAPKU日历中的日期检查NSDate

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

Stack Overflow用户
提问于 2011-10-26 13:05:55
回答 2查看 1.4K关注 0票数 0

我使用核心数据来存储对象,我的project.There是对象中的一个日期字段,.I想要检查iphone的Tapku日历组件的点击日期。我的对象上的日期格式为

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

如何将日期与tapku日历控件返回的日期进行核对。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-23 08:58:27

是的,你可以,这是我做了什么让它工作:

在方法上:(NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate

  • 执行正常的核心数据获取,以获取对象数组
  • 使用NSArray indexesOfObjectsPassingTest: method
  • 开始每天从startDate到lastDate的检查对于所有通过测试的对象,将and object添加到标记数组中,并添加一个字典,将测试日期作为关键字,将获取的对象作为值

警告:确保将您的NSDate对象强制转换为no time和GMT:0日期,以便使日期比较起作用(通过艰苦的方式学会了这一点...)

下面是一些代码。

代码语言:javascript
复制
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

票数 3
EN

Stack Overflow用户

发布于 2011-10-26 14:09:18

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

代码语言:javascript
复制
if ([myDate isSameDay:returnedDate])
    // Do something here

这就是你要找的吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7898895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档