プロジェクトのオブジェクトを保存するためにコアデータを使用しています。オブジェクトに日付フィールドがあります。iPhone用のTapkuカレンダーコンポーネントのクリックされた日付と照合したいと思います。オブジェクトの日付は次の形式です
2011-01-10そしてそれはローカライズされた日付です。
Tapkuカレンダーコントロールから返された日付に対して日付を確認するにはどうすればよいですか。
はい、できます。これを機能させるために私が行ったことは次のとおりです。
メソッドについて:(NSArray *)calendarMonthView:(TKCalendarMonthView *)monthView markFromDate:(NSDate *)startDate toDate:(NSDate *)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
もう少し情報を使用することもできますが、tapkuライブラリにはNSDateにisSameDayという拡張メソッドがあるため、次のように使用できます。
if ([myDate isSameDay:returnedDate])
// Do something here
それはあなたが探しているものですか?