Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all NSDates BETWEEN startDate and endDate

My question is the REVERSE of the typical "How do I find out if an NSDate is between startDate and endDate?"

What I want to do is find ALL NSDATES (to the day, not hour or minute) that occur BETWEEN startDate and endDate. Inclusive of those dates would be preferable, although not necessary.

Example: (I know that these don't represent NSDates, these are just for illustration)

INPUT: startDate = 6/23/10 20:11:30 endDate = 6/27/10 02:15:00

OUTPUT: NSArray of: 6/23/10, 6/24/10, 6/25/10, 6/26/10, 6/27/10

I don't mind doing the work. It's just that I don't know where to start in terms of making efficient code, without having to step through the NSDates little by little.

like image 561
Daniel Amitay Avatar asked Jun 26 '10 18:06

Daniel Amitay


1 Answers

Use an NSCalendar instance to convert your start NSDate instance into an NSDateComponents instance, add 1 day to the NSDateComponents and use the NSCalendar to convert that back to an NSDate instance. Repeat the last two steps until you reach your end date.

like image 175
Johan Kool Avatar answered Sep 23 '22 02:09

Johan Kool