views:

313

answers:

1

For those familiar with Excel, I'm trying to use a similiar NETWORKDAYS function within Cocoa.

Can anyone help with the type of info I'll need to construct an NSDate catagory that can give me only the working days betweek two dates?

many thanks Nik

A: 

Hi,

I know that I'll give is not optimized, but it's just to give you a way to explore. You can use the NSCalendar and the NSDateComponents like that:

// Date of today
NSDate *today = [NSDate date];
// init the gregorian calendar
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Retrieve the NSDateComponents for the current date
NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
// Number of the day in the week (e.g 2 = Monday)
NSInteger weekday = [weekdayComponents weekday];

(see Calendars, Date Components, and Calendar Units)

From there you start from your first date and you iterate this for each day until your end date and using the weekday you can determine if the day is during a weekend or not. (I repeated that's not optimized but it's just a track)

Yannick L.
thanks Yannick.If anyone knows of a more efficient way than enumerating each nsdate instance to check for a weekday value then I'd appreciate the it.
Burnsoft Ltd