如何获得下个月的第1,第2,第3,第4个工作日(工作日)

时间:2014-04-30 10:19:40

标签: ios objective-c nsdate nsdatecomponents

我一直在努力寻找下个月第1,第2,第3或第4个工作日/工作日(工作日)的日期。

我有以下代码:

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);

任何人都有任何提示从哪里开始?

谢谢

2 个答案:

答案 0 :(得分:3)

我认为这可以解决您的问题:

    // 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);
    }

答案 1 :(得分:1)

这里有一个C函数可以完成你想做的事情 - 请注意你必须用[NSString stringWithCString ...]将C字符串转换为NSStrings;还记得在这个例子中free()nextMonthWeekday。

#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;
}