Question

I've got a task to calculate how many days are left until a certain deadline expires. For example: I have a 10 business days deadline starting today (let's assume it's a Monday). I need to calculate how many calendar days remain until that deadline expires. Which means I need to see how many weekends fall into this time period and add them to the "remaining days" result (which in our concrete example should be 12 days since only one weekend will fall in this timeframe).

How do I do that on iOS?

Let me know if you need more clarification

Was it helpful?

Solution

int numberOfDays=30;

NSDate *startDate=[NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *offset = [[NSDateComponents alloc] init];
NSMutableArray *dates = [NSMutableArray arrayWithObject:startDate];
NSMutableArray *weekends = [NSMutableArray new];
NSMutableArray *businessDays = [NSMutableArray new];

if (![self isDateWeekend:startDate]) {
    [businessDays addObject:startDate];
}

for (int i = 1; i < numberOfDays; i++) {
    [offset setDay:i];
    NSDate *nextDay = [calendar dateByAddingComponents:offset toDate:startDate options:0];
    [dates addObject:nextDay];
    [businessDays addObject:nextDay];
    if ([self isDateWeekend:nextDay]) {
        [weekends addObject:nextDay];
        [businessDays removeObject:nextDay];
    }
}

NSLog(@"All Days%@",dates);
NSLog(@"All Weekends%@",weekends);
NSLog(@"Business Days%@",businessDays);

The weekend function has been borrowed from an other SO answer as it was neater,

- (BOOL) isDateWeekend:(NSDate*)date
{
    NSRange weekdayRange = [[NSCalendar currentCalendar] maximumRangeOfUnit:NSCalendarUnitWeekday];
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:date];
    NSUInteger weekdayOfDate = [components weekday];
    if (weekdayOfDate == weekdayRange.location || weekdayOfDate == weekdayRange.length) {
            //the date falls somewhere on the first or last days of the week
            return YES;
        }
        else
            return NO;
 }

This can be extended to get all days,weekends and business days within a date range.

OTHER TIPS

do some calculation here . and try like this .

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents     = [gregorian components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];

//calcuate number of days to substract from today
NSDateComponents *componentsToSubtract  = [[NSDateComponents alloc] init];
[componentsToSubtract setDay: (0 - ((dayofweek + 5) % 7))];
[componentsToSubtract setHour: 0 - [weekdayComponents hour]];
[componentsToSubtract setMinute: 0 - [weekdayComponents minute]];
[componentsToSubtract setSecond: 0 - [weekdayComponents second]];

//Create date for first day in week
NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];


NSDateComponents *componentsToAdd = [gregorian components:NSDayCalendarUnit fromDate:beginningOfWeek];
[componentsToAdd setDay:6];

NSDate *endOfWeek = [gregorian dateByAddingComponents:componentsToAdd toDate:beginningOfWeek options:0]

Try this,

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear| NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekDay = components.weekday;
int numberOfweeks = numberOfDays/5;
int weekDayDiff = 7 - weekDay;
if (weekDayDiff >= 5 && numberOfDays%5 == 0) {
    numberOfweeks --;
}
numberOfDays += numberOfweeks * 2;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top