Thus, your definition of working days seems Monday to Friday. Let me call these "working days", as opposed to "weekends" (which are Saturday and Sunday).
, startDate endDate, : startDate endDate. "" , .
startDate endDate , . , , startDate, , endDate, . , "" .
, , , 5, . startDate endDate. startDate - , endDate - , , , .
? startDate. , startDate (0 , 1 , 2 , 3 , 4 5 ). endDate. , endDate , endDate, (5 , 4 , 3 , 2 , 1 0 ).
, startDate endDate
5 * inclusive count of weeks from startDate to endDate
- count of workdays preceding startDate
- count of workdays following and including endDate
, , 1, startDate - , endDate - . 2 ( , endDate , ),
5 * inclusive count of weeks from startDate to endDate
- count of workdays preceding startDate
- count of workdays following (but not including) endDate
Objective-C Cocoa . NSCalendar:
@interface NSCalendar (Rob_Workdays)
- (NSInteger)Rob_countOfWorkdaysFromDate:(NSDate *)startDate
toDate:(NSDate *)endDate;
- (NSInteger)Rob_countOfWorkdaysFromDate:(NSDate *)startDate
toAndIncludingDate:(NSDate *)endDate;
@end
, :
@implementation NSCalendar (Rob_Workdays)
- (NSInteger)Rob_countOfWorkdaysFromDate:(NSDate *)startDate toDate:(NSDate *)endDate {
return 5 * [self Rob_inclusiveCountOfWeeksFromDate:startDate toDate:endDate]
- [self Rob_countOfWorkdaysPrecedingDate:startDate]
- [self Rob_countOfWorkdaysFollowingAndIncludingDate:endDate];
}
- (NSInteger)Rob_countOfWorkdaysFromDate:(NSDate *)startDate toAndIncludingDate:(NSDate *)endDate {
return 5 * [self Rob_inclusiveCountOfWeeksFromDate:startDate toDate:endDate]
- [self Rob_countOfWorkdaysPrecedingDate:startDate]
- [self Rob_countOfWorkdaysFollowingDate:endDate];
}
, , . , startDate , , endDate . startDate endDate 7:
- (NSInteger)Rob_inclusiveCountOfWeeksFromDate:(NSDate *)startDate
toDate:(NSDate *)endDate
{
startDate = [self Rob_sundayOnOrBeforeDate:startDate];
endDate = [self Rob_sundayAfterDate:endDate];
NSDateComponents *components = [self components:NSCalendarUnitDay
fromDate:startDate toDate:endDate options:0];
return components.day / 7;
}
:
- (NSDate *)Rob_sundayOnOrBeforeDate:(NSDate *)date {
return [self Rob_dateByAddingDays:1 - [self Rob_weekdayOfDate:date]
toDate:date];
}
:
- (NSDate *)Rob_sundayAfterDate:(NSDate *)date {
return [self Rob_dateByAddingDays:8 - [self Rob_weekdayOfDate:date]
toDate:date];
}
( 1 = 7 = ) :
- (NSInteger)Rob_weekdayOfDate:(NSDate *)date {
return [self components:NSCalendarUnitWeekday fromDate:date].weekday;
}
:
- (NSDate *)Rob_dateByAddingDays:(NSInteger)days toDate:(NSDate *)date {
if (days != 0) {
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = days;
date = [self dateByAddingComponents:components toDate:date
options:0];
}
return date;
}
, days , .
, , ( ), , , , :
- (NSInteger)Rob_countOfWorkdaysPrecedingDate:(NSDate *)date {
switch ([self Rob_weekdayOfDate:date]) {
case 1: return 0;
case 2: return 0;
case 3: return 1;
case 4: return 2;
case 5: return 3;
case 6: return 4;
case 7: return 5;
default: abort();
}
}
, , , , , :
- (NSInteger)Rob_countOfWorkdaysFollowingAndIncludingDate:(NSDate *)date {
switch ([self Rob_weekdayOfDate:date]) {
case 1: return 5;
case 2: return 5;
case 3: return 4;
case 4: return 3;
case 5: return 2;
case 6: return 1;
case 7: return 0;
default: abort();
}
}
- (NSInteger)Rob_countOfWorkdaysFollowingDate:(NSDate *)date {
switch ([self Rob_weekdayOfDate:date]) {
case 1: return 5;
case 2: return 4;
case 3: return 3;
case 4: return 2;
case 5: return 1;
case 6: return 0;
case 7: return 0;
default: abort();
}
}
@end