In my program, I have an NSArray of various dates and times, stored as strings and formatted like this: @[@"07:23",@"18:09",@"13:55"];
When I use an NSDateFormatter to convert these to NSDates, the times are correct, but year/month/day information is added.
The arrays that I have created are columns of a bus schedule. Each entry is one timeslot for whatever stop the array represents. My application needs to take the current time: [NSDate date] and see which time from the array is next in sequence. I'm just trying to display when the very next bus will arrive.
I have thought of comparing each element of the array with the current date and time using -[NSDate's laterDate:], but the problem is that when I convert the strings to NSDate objects, it gives them some random day-month-year like 13:55:00 January 1st, 2001 which will always be before the current date, so my test won't work.
I can find some workarounds that are really tragically McGuyvered but I would prefer something clean.
What I want to know are these things:
- Can I remove the day/month/year portion from the
NSDate? - Is it possible to easily set the day/month/year of each object in my array to today without using
NSDateComponentsandNSCalendar? I can manipulate them as they enter the array. - Would it be easier to reformat the current date/time to match the day/month/year of the array?
- Otherwise, is there a better, cleaner solution to find the next upcoming timeslot? I am open to changing the entire format from arrays if necessary.
NSDateComponentsandNSCalendar? - Enrico Susatyo