How do I find the Next Closest Date to today from a list of dates in a Plist on iOS?
- by user1173823
Situation: In short, I have a football schedule. I would like to use a custom cell which provides more info for only the next game date in the schedule.
Issue: How do I find only the next closest game in the schedule (for iOS)?
I've watched the WWDC 2013 video for "Solutions to Common Date and Time Issues" however this primarily applies to the Mac. I've searched numerous posts here and some are close but not what I need to find ONLY the next date from my list of dates in the schedule. From other posts I see where I can compare two specific dates, but this is not what I want to do. I want to find the next closest date that is equal to or after today from a list of dates.
This is where I am now.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Populate the table from the plist
NSDictionary *season = _schedContentArray[indexPath.section];
NSArray *schedule = season[@"Schedule"];
NSDictionary *game = schedule[indexPath.row];
//find the closest game date after today's date ??
NSString *gameDateStr = game[@"GameDate"];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.calendar=calendar;
[dateFormatter setDateFormat:@"MM/dd/yy"];
NSDate *today = [NSDate date];
NSDate *gameDate = [dateFormatter dateFromString:gameDateStr];
//NSString *nextGame =
NSLog(@"game date is %@",gameDate);
The NSLog returns the game dates (except for the open date):
2013-11-11 16:10:05.979 Clemson Football[24060:70b] game date is 2013-08-31 04:00:00 +0000
2013-11-11 16:10:05.982 Clemson Football[24060:70b] game date is 2013-09-07 04:00:00 +0000
2013-11-11 16:10:05.985 Clemson Football[24060:70b] game date is (null)
2013-11-11 16:10:05.987 Clemson Football[24060:70b] game date is 2013-09-19 04:00:00 +0000
2013-11-11 16:10:05.988 Clemson Football[24060:70b] game date is 2013-09-28 04:00:00 +0000
2013-11-11 16:10:05.990 Clemson Football[24060:70b] game date is 2013-10-05 04:00:00 +0000
2013-11-11 16:10:05.992 Clemson Football[24060:70b] game date is 2013-10-12 04:00:00 +0000
2013-11-11 16:10:05.993 Clemson Football[24060:70b] game date is 2013-10-19 04:00:00 +0000
2013-11-11 16:10:05.995 Clemson Football[24060:70b] game date is 2013-10-26 04:00:00 +0000
2013-11-11 16:10:05.996 Clemson Football[24060:70b] game date is 2013-11-02 04:00:00 +0000
2013-11-11 16:10:05.998 Clemson Football[24060:70b] game date is 2013-11-09 05:00:00 +0000
2013-11-11 16:10:06.000 Clemson Football[24060:70b] game date is 2013-11-14 05:00:00 +0000
2013-11-11 16:10:06.001 Clemson Football[24060:70b] game date is 2013-11-23 05:00:00 +0000
2013-11-11 16:10:06.003 Clemson Football[24060:70b] game date is 2013-11-30 05:00:00 +0000
2013-11-11 16:10:06.005 Clemson Football[24060:70b] game date is 2013-12-07 05:00:00 +0000
Thanks in advance for any assistance you can provide. This seems like it should be simple but has been fairly frustrating. Let me know if you need additional info.