1
votes

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:

  1. Can I remove the day/month/year portion from the NSDate?
  2. Is it possible to easily set the day/month/year of each object in my array to today without using NSDateComponents and NSCalendar? I can manipulate them as they enter the array.
  3. Would it be easier to reformat the current date/time to match the day/month/year of the array?
  4. 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.
1
Why can't you use NSDateComponents and NSCalendar? - Enrico Susatyo
I definitely can, but I have dozens of mutable arrays to make out of literal arrays and I would prefer to not have to add that much complexity to each FOR loop. So I was thinking there is probably a cleaner way - SeniorShizzle
those operations are quite cheap, I think. It's what I'd use. - Enrico Susatyo
What do you think about using those variables to set the current date to January 1st, 2000 instead of setting each array element to today? I think this would also work better in the case that the application didn't repopulate the arrays the next day because it was left open. - SeniorShizzle

1 Answers

3
votes

Can I remove the day/month/year portion from the NSDate?

No. An NSDate is merely an instant in time that is some number of seconds since some reference date. Describing that instant in time as some year/month/day depends on the local calendar. For example, the "day of month" of [NSDate date] as I type this is 28 where I live but 29 for the same NSDate value in Japan.

Is it possible to easily set the day/month/year of each object in my NSMutableArray to today? without using NSDateComponents and NSCalendar?

No. That's what NSDateComponents is for.

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.

Use NSCalendar's -components:fromDate: to get an NSDateComponents object that matches [NSDate date]. Replace the hour/minute/second components with an arrival time's hour/minute/second: this is an arrival time today. Add one to the day component: this is an arrival time tomorrow. (Weekend and holiday schedules cause extra complication; the weekday component may be useful.) Convert back to NSDate using NSCalendar's -dateFromComponents: and perform your date comparisons there.