如何检查NSDate与TAPKU日历中的日期 [英] How to check NSDate against a date in TAPKU calendar

查看:141
本文介绍了如何检查NSDate与TAPKU日历中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



解决方案

可以,这里是我的(NSArray *)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate(NSDate *)的方法:




  • 使正常的核心数据获取获取对象数组

  • 开始每天检查startDate to lastDate with the [NSArray indexesOfObjectsPassingTest:] method

  • 对于所有通过测试add和object到marks数组,并添加一个测试日作为键的字典和获取的对象值



警告:请确保将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];

//初始偏移组件,每次增加循环中的天数
NSDateComponents * offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];


while(YES){
//日期是否超过上一个日期?如果是,退出循环。
// NSOrderedDescending =左边的值大于右边
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:indices] forKey:ds];


} else {
//在这个日期没有wods
[self.dataArray addObject:[NSNumber numberWithBool:NO]];使用偏移量组件增量一天(例如,在这种情况下为1天)
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


I am using core data to store objects for my project.There is a date field in the object .I want to check against the clicked date of the Tapku calendar component for iphone. The date on my object is in the format

2011-01-10 and it is a localized date .

How can i check the date against the date returned from the tapku calendar control.

解决方案

Yes you can, here is what I did to make it work:

on the method: (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate

  • Make the normal core data fetch to get an array of objects
  • Start checking every day from startDate to lastDate with the [NSArray indexesOfObjectsPassingTest:] method
  • For all that passes the test add and object to the marks array and add a dictionary with the tested day as key and the fetched objects as values

WARNING: make sure you cast your NSDate objects to a no time and GMT:0 dates in order to make the day comparison to work (learned this the hard way...)

Here is some code...

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];

The midnightUTC method can be found here: http://www.voidasterisk.org/post/4209842795/how-to-calculate-nsdate-of-midnight-in-ios

这篇关于如何检查NSDate与TAPKU日历中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆