How to use the search bar for differentiate the data based on time?
iam developing one application.In that i use the search bar for search the data.But my search is perform based on the time.So how to write the code for searching the data based on time.For normal string search i write the code like below.
NSRange namerange=[databasefield.name rangeOfString:s开发者_如何学编程rchbar.text options:NSCaseInsensitiveSearch];
NSRange adrsrange=[databasefield.address rangeOfString:srchbar.text options:NSCaseInsensitiveSearch];
//NSRange timerange=[databasefield.insert_datetime rangeOfString:srchbar.text options:NSCaseInsensitiveSearch];
if ((namerange.length>0)||(adrsrange.length>0))
{
[copydata addObject:databasefield];
}
In the above code in comments i written the code for search the data based on time.But it gives the error.So please tell me how to get this one.
If I understand correctly your problem, you can make it like here:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSDate *searchDate = [dateFormatter dateFromString:srchbar.text];
[dateFormatter release];
for (int i = 0; i < [self.data count]; i++) {
NSDate *date = [self.data objectAtIndex:i];
NSInteger result = [self compareTwoTimes:date date2:searchDate];
if (result == NSOrderedSame)
[self.filteredArray addObject:[self.data objectAtIndex:i]];
}
Where
+ (NSInteger) compareTwoTimes:(NSDate *) date1 date2:(NSDate *) date2
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm:ss"];
NSString *dateString1 = [dateFormat stringFromDate:date1];
NSString *dateString2 = [dateFormat stringFromDate:date2];
NSDate *d1 = [dateFormat dateFromString:dateString1];
NSDate *d2 = [dateFormat dateFromString:dateString2];
[dateFormat release];
return [d1 compare:d2];
}
精彩评论