Working days between two dates in objective-c?

I looked through everything and did not find a function that gives the number of working days between two dates.

How should I do it?

Of course, this will not take into account the holidays, since each country has its own, but what can I do with some specific functions for my country.

EDIT: I did not ask how to get the number of days between two dates.

-1
source share
1 answer

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)

/** I return the number of non-weekend days from startDate to endDate,
    including the day containing `startDate` and excluding the day
    containing `endDate`. */
- (NSInteger)Rob_countOfWorkdaysFromDate:(NSDate *)startDate
    toDate:(NSDate *)endDate;

/** I return the number of non-weekend days from startDate to endDate,
    including the day containing `startDate` and the day containing
    `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:

/** I return the number of weeks from `startDate` to `endDate`, including
    the weeks containing each date (but only counting the week once if the
    same week includes both dates). */
- (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();
    }
    // equivalently: return MIN(MAX(0, [self Rob_weekdayOfDate:date] - 2), 5);
}

, , , , , :

- (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();
    }
    // equivalently: return MIN(7 - [self Rob_weekdayOfDate:date], 5);
}

- (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();
    }
    // equivalently: return MAX(6 - [self Rob_weekdayOfDate:date], 0);
}

@end
+3

All Articles