2011-10-26 86 views
0

我正在使用核心數據來存儲我的項目的對象。對象中有一個日期字段。我想檢查Tapku日曆組件的點擊日期。我的對象上的日期格式爲如何在TAPKU日曆中對照日期檢查NSDate

2011-01-10並且它是本地化的日期。

我該如何檢查從tapku日曆控件返回的日期。

回答

0

可以用更多的信息,但tapku圖書館做對NSDate的擴展方法稱爲isSameDay所以你可以使用:

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

是你在找什麼?

2

可以呀,這裏是我做過什麼,使其工作:

的方法:(NSArray的*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate的*)的startDate TODATE:(NSDate的*) lastDate

  • 使正常核心數據提取來獲得對象的數組
  • 開始檢查來自的startDate每天與到lastDate [NSArray的indexesOfObjectsPassingTest:]方法
  • 對於全部通過測試添加和對象到標記arr唉,並添加一個詞典與測試一天鍵和獲取的對象作爲值

警告:確保你投你的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