Question

I have been trying to find what is the date of the 1st, 2nd, 3rd or 4th business/working day (weekday) for next month.

I have the following code:

NSTimeInterval *lastDue; // unix time stamp from last due date
NSDate *ldue=[NSDate dateWithTimeIntervalSince1970:lastDue];                
NSDateComponents *dc =[calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekdayCalendarUnit|NSWeekdayOrdinalCalendarUnit) fromDate:ldue];

NSLog(@"firstWeekday: %ld",(long)dc.weekday);

Anyone has any tips where to start?

Thank you

Was it helpful?

Solution

I think this will solve your problem:

    // Get the date of today
    NSDate *today = [[NSDate alloc] init];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:today];
    // Set the next month, you don't have t worry in December, it will automatically switch to the next year
    components.month = components.month+1;
    // Iterate over the first four days of the month
    for(int i=1;i<5;i++)
    {
        components.day = i;
        // Get the new date
        NSDate *dayInMonth = [gregorian dateFromComponents:components];
        // Get the day in the week
        NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:dayInMonth];
        NSInteger weekday = [weekdayComponents weekday];
        // Sunday = 1, Monday = 2, ...
        BOOL isWeekDay = YES;
        if(weekday==7||weekday==1) {
            isWeekDay = NO;
        }
        // Format properly the output
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"dd/MM/yy - hh:mm a"];
        NSString *dateWithNewFormat = [formatter stringFromDate:dayInMonth];
        // Print the date and 1 if is a weekday or 0 if not
        NSLog(@"'%@': %d",dateWithNewFormat, isWeekDay);
    }

OTHER TIPS

Here's a C function that will do what you want - take note you'll have to convert the C strings to NSStrings with [NSString stringWithCString...]; also remember to free() nextMonthWeekday in this example.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

// whichWeekday is how far into the next month you want to go
// for Example 1 is first weekday, 3 is third weekday
char *getNextMonthWeekday(int whichWeekday) {
    time_t t = time(NULL);
    struct tm *current = localtime(&t), *next = localtime(&t);
    if (current->tm_mon < 11) {
        next->tm_mon++;
    } else {  // if started in december, next month is january
        next->tm_year++;
        next->tm_mon = 0;
    }
    next->tm_mday = 1;  // first day of month
    for (int i = 0; i < whichWeekday; next->tm_mday++) {
        time_t u = mktime(next);
        next = localtime(&u);
        while (next->tm_wday == 0 || next->tm_wday == 6) {
            next->tm_mday++;
            time_t v = mktime(next);
            next = localtime(&v);
        }
        i++;
    }
    char *weekday = malloc(32);
    strftime(weekday, 32, "%A", next);
    return weekday;
}

int main(int argc, const char * argv[])
{
    char *nextMonthWeekday = getNextMonthWeekday(4);
    printf("%s", nextMonthWeekday);
    free(nextMonthWeekday);
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top