Question

I'm trying get only the Saturdays and Sundays between two dates, but I don't know why get me free days on a week.

Here is my code:

- (BOOL)checkForWeekend:(NSDate *)aDate {
    BOOL isWeekendDate = NO;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange weekdayRange = [calendar maximumRangeOfUnit:NSWeekdayCalendarUnit];
    NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:aDate];
    NSUInteger weekdayOfDate = [components weekday];

    if (weekdayOfDate == weekdayRange.location || weekdayOfDate == weekdayRange.length) {
        // The date falls somewhere on the first or last days of the week.
        isWeekendDate = YES;
    }
    return isWeekendDate;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSString *strDateIni = [NSString stringWithString:@"28-01-2012"];
    NSString *strDateEnd = [NSString stringWithString:@"31-01-2012"];

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd-MM-yyyy"];
    NSDate *startDate = [df dateFromString:strDateIni];
    NSDate *endDate = [df dateFromString:strDateEnd];

    unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate  toDate:endDate  options:0];

   // int months = [comps month];
    int days = [comps day];

    for (int i=0; i<days; i++) 
    {

        NSTimeInterval interval = i;
        NSDate * futureDate = [startDate dateByAddingTimeInterval:interval];

        BOOL isWeekend = [self checkForWeekend:futureDate]; // Any date can be passed here.

        if (isWeekend) {
            NSLog(@"Weekend date! Yay!");
        }
        else
        {
            NSLog(@"Not is Weekend");
        }


    }

}

The problem: The issue was caused by NSTimeInterval interval = i; The logic of the for loop was to iterate by days. Setting the time interval to i was iterating by seconds.

From documentation on NSTimeInterval

NSTimeInterval is always specified in seconds;

The answer:

Changing the NSTimeInterval line to

NSTimeInterval interval = i*24*60*60;
Was it helpful?

Solution

Here is a link to another answer I posted on SO (shameless, I know). It has some code that may help you with dates in the future. The methods are implemented as categories of NSDate, meaning they become methods of NSDate.

There are several functions there that help with weekends. But these two might be most helpful:

- (NSDate*) theFollowingWeekend;
- (NSDate *) thePreviousWeekend;

They return the date of the weekend following and prior to the receiver (self).

Generally, you should not use the notion that a day is 86400 seconds, and should use NSDateComponents and NSCalendar. This works even when daylight savings time transitions occur between dates. Like this:

- (NSDate *) dateByAddingDays:(NSInteger) numberOfDays {
    NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
    dayComponent.day = numberOfDays;

    NSCalendar *theCalendar = [NSCalendar currentCalendar];
    return [theCalendar dateByAddingComponents:dayComponent toDate:self options:0];
}

OTHER TIPS

One very important thing to remember is that one day is not (necessarily) equal to 24*60*60 seconds. And you should not do date arithmetic yourself

What you really need to do might seem a little tedious but this is the correct thing to do: use NSCalendar and – dateByAddingComponents:toDate:options:

See Calendrical Calculations guide.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top