Note that there are some explanatory texts on larger screens.

plurals
  1. POHow To Search For NSDate?
    primarykey
    data
    text
    <p>I have an NSDate I want to search for in my predicate. However, the timing may not be <code>2009-12-06 00:00:00 +0000</code> but may look more like <code>2009-12-06 3:05:90 +0000</code>. So how can I search for any date that fits within the bounds of that day (24 hours).</p> <p>Edit:</p> <p>These are the related methods to the crash:</p> <p>Crash is happening in <a href="https://github.com/devinross/tapkulibrary/blob/master/src/TapkuLibrary/TKCalendarMonthView.m#L469" rel="nofollow"><code>this method</code></a> related to the Calendar's API framework</p> <pre><code>- (void) reactToTouch:(UITouch*)touch down:(BOOL)down { ... if ([marks count] &gt; 0) { if([[marks objectAtIndex: row * 7 + column] boolValue]) [self.selectedImageView addSubview:self.dot]; else [self.dot removeFromSuperview]; }else{ [self.dot removeFromSuperview]; } ... } </code></pre> <p>This graph sets the graph's date and dots for the date if there is data for that date</p> <pre><code>- (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate { NSLog(@"calendarMonthView marksFromDate toDate"); NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Session" inManagedObjectContext:self.managedObjectContext]; [request setEntity:entity]; [request setResultType:NSDictionaryResultType]; [request setReturnsDistinctResults:YES]; [request setPropertiesToFetch :[NSArray arrayWithObject:@"timeStamp"]]; NSError *error; NSArray *objects = [managedObjectContext executeFetchRequest:request error:&amp;error]; NSArray *tempData = [objects valueForKey:@"timeStamp"]; NSMutableArray * data1 = [NSMutableArray array]; NSCalendar * gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; NSUInteger flags = ( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ); NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss Z"]; [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; [tempData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSDate * date = [gregorian dateFromComponents:[gregorian components:flags fromDate:obj]]; [data1 addObject:[formatter stringFromDate:date]]; }]; self.data = data1; // Initialise empty marks array, this will be populated with TRUE/FALSE in order for each day a marker should be placed on. NSMutableArray *marks = [NSMutableArray array]; // Initialise calendar to current type and set the timezone to never have daylight saving NSCalendar *cal = [NSCalendar currentCalendar]; [cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSDateComponents *comp = [cal components:(NSMonthCalendarUnit | NSMinuteCalendarUnit | NSYearCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSSecondCalendarUnit) fromDate:startDate]; NSDate *d = [cal dateFromComponents:comp]; // Init offset components to increment days in the loop by one each time NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; [offsetComponents setDay:1]; // for each date between start date and end date check if they exist in the data array while (YES) { // Is the date beyond the last date? If so, exit the loop. // NSOrderedDescending = the left value is greater than the right if ([d compare:lastDate] == NSOrderedDescending) { break; } // If the date is in the data array, add it to the marks array, else don't if ([data containsObject:[d description]]) { [marks addObject:[NSNumber numberWithBool:YES]]; } else { [marks addObject:[NSNumber numberWithBool:NO]]; } // Increment day using offset components (ie, 1 day in this instance) d = [cal dateByAddingComponents:offsetComponents toDate:d options:0]; } [offsetComponents release]; [request release]; return [NSArray arrayWithArray:marks]; } </code></pre> <p>And finally, this is the method we came up with, that pushes to the detail view when a date is selected:</p> <pre><code>- (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d { NSLog(@"calendarMonthView didSelectDate"); NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; NSString * dateString = [formatter stringFromDate:d]; if ( [data containsObject:dateString] ) { NSDate * searchDate = [formatter dateFromString:dateString]; NSCalendar * calendar1 = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; NSDateComponents * dateComponents = [[[NSDateComponents alloc] init] autorelease]; [dateComponents setDay:1]; NSDate * searchDateEnd = [calendar1 dateByAddingComponents:dateComponents toDate:searchDate options:0]; NSFetchRequest *request = [[NSFetchRequest alloc]init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Session" inManagedObjectContext:self.managedObjectContext]; [request setEntity:entity]; NSPredicate * predicate1 = [NSPredicate predicateWithFormat:@"timeStamp &gt;= %@ AND timeStamp &lt; %@", searchDate, searchDateEnd]; [request setPredicate: predicate1]; [request setFetchBatchSize:20]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; [request setSortDescriptors:sortDescriptors]; NSError *error = nil; NSArray *array = [managedObjectContext executeFetchRequest:request error:&amp;error]; NSLog(@"xxx array: %@", array); SessionViewController *sessionViewController = [[SessionViewController alloc] initWithNibName:@"SessionViewController" bundle:nil]; self.selectedSession = (Session *)[array objectAtIndex:0]; sessionViewController.selectedSession = self.selectedSession; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MMM d, y"]; NSString *dateString = [dateFormatter stringFromDate:selectedSession.timeStamp]; sessionViewController.title = dateString; sessionViewController.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:sessionViewController animated:YES]; [sessionViewController release]; [dateFormatter release]; [sortDescriptor release]; [request release]; [sortDescriptors release]; } } </code></pre> <p>Edit:</p> <p>When I log the the dates,</p> <p>The rawDate is straight <code>session.timeStamp</code> and dateString has a date formatter of <code>[dateFormatter setDateFormat:@"eeee, MMM d, y"];</code></p> <pre><code>2011-06-27 20:13:44.936 Curl[18714:707] dateString: Monday, Jun 27, 2011 2011-06-27 20:13:44.941 Curl[18714:707] rawString: 2011-06-27 07:05:01 +0000 2011-06-27 20:13:44.961 Curl[18714:707] dateString: Thursday, Jun 23, 2011 2011-06-27 20:13:44.965 Curl[18714:707] rawString: 2011-06-23 08:12:49 +0000 2011-06-27 20:13:44.977 Curl[18714:707] dateString: Wednesday, Jun 22, 2011 2011-06-27 20:13:44.983 Curl[18714:707] rawString: 2011-06-22 21:23:08 +0000 2011-06-27 20:13:44.993 Curl[18714:707] dateString: Tuesday, Jun 21, 2011 2011-06-27 20:13:44.997 Curl[18714:707] rawString: 2011-06-22 03:23:51 +0000 2011-06-27 20:13:45.007 Curl[18714:707] dateString: Monday, Jun 20, 2011 2011-06-27 20:13:45.011 Curl[18714:707] rawString: 2011-06-21 01:03:53 +0000 2011-06-27 20:13:45.020 Curl[18714:707] dateString: Friday, Jun 17, 2011 2011-06-27 20:13:45.024 Curl[18714:707] rawString: 2011-06-18 01:03:53 +0000 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload