Cocoa - Calculate business days between two dates

I am new to cocoa programming and I want to know how I get the number of working days between dates. So only Monday through Friday. Thank.

+1
date objective-c cocoa nsdate
Aug 27 '14 at 10:34
source share
1 answer
NSDate *startDate = ...; NSDate *stopDate = ...; NSDateFormatter *df = [NSDateFormatter new]; df.dateFormat = @"yyyy-MM-dd"; startDate = [df dateFromString:[df stringFromDate:startDate]]; // get start of the day NSDateComponents *comps = [NSDateComponents new]; comps.day = 1; // one day in NSDateComponents NSUInteger count = 0; while ([stopDate timeIntervalSinceDate:startDate] >= 0) { NSInteger weekday = [[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:startDate].weekday; if (weekday != 1 && weekday != 6) ++count; // filter out weekend days - 6 and 1 startDate = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:startDate options:0]; // increment start day } 
+3
Aug 27 '14 at 12:14
source share



All Articles