views:

260

answers:

2

A friend asked me yesterday if this was possible on the iPhone. I took a look at NSCalendar and all the related Classes but couldn't find a solution to this.

So I thought about this approach: If I had two dates dateA and dateB, I would have to make a for-loop and iterate over every single day in this interval. Then I would count the business days monday until friday, and return the result.

Then I went to bed, and I woke up with this probably much better idea: I need to know what day is it when I start. Lets say it's thursday. And then I must know how many days are in that interval. The last part is not hard to figure out. For the first part, I have no clue yet, but I believe there's an day of week value in NSCalendar. With that, I could do some simple math to calculate the amount of business days.

Did anyone do that already on the iPhone?

+2  A: 

How many times are you performing this calculation? Do the dates you'll be needing have to fall in a specific range?

If you're performing lots of these business date differences, consider building a lookup table that will store the number of business days between that date and some arbitrary date. Then to find the number of business days between two dates just subtract those lookup values. As for day of week calculations... remember that not all weekdays are "business days", so you'd need to somehow account for holidays.

Yuliy
that's right, holidays would be the next problem. but in this case it would be sufficient (for the beginning) to have the number of business days. The dates A and B can be arbitrary really. I assume that the seqjuence of monday to sunday never changes, there will never ever follow two same named days after eachother and there will be never a gap in this sequence. At least until someone decides to change our calendar system, as it happened a lot of times in the past.
dontWatchMyProfile
If you just want the number of weekdays between two dates, just find the number of weeks between them and multiply by 5. Now you'll need to handle the remaining few days, which you can just do in a simple loop ("given this start date, how many of the next N days are weekdays, where N is between 0 and 6?")
Yuliy
+1  A: 

Here are the pieces with which to do this calculation:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

// Determine the integer number of weeks between the two dates
NSDateComponents *components = [calendar components:NSWeekCalendarUnit fromDate:firstDate toDate:secondDate options:0];
NSInteger numWeeks = [components week];

// Determine the day of the week for each date
components = [calendar components:NSWeekdayCalendarUnit fromDate:firstDate];
NSInteger firstWeekday = [components weekday];
components = [calendar components:NSWeekdayCalendarUnit fromDate:secondDate];
NSInteger secondWeekday = [components weekday];

[calendar release];

I leave it as an exercise for the reader to do the necessary math to work out the number of business days from these values.

glorifiedHacker