iOS计算N个工作日之后的日期和计算N个月之后的日期

项目需要 计算3个工作日之后的日期和N(用户输入)个月之后的日期

先看计算N个月之后的日期


- (NSString *)afterMouth:(NSString *)mouth timeStr:(NSString *)timeStr{
    NSString *birthdayStr = timeStr;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *birthdayDate = [dateFormatter dateFromString:birthdayStr];
    NSDate *date = [self getPriousorLaterDateFromDate:birthdayDate withMonth:[mouth integerValue]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strDate = [dateFormatter stringFromDate:date];
    return strDate;
}

-(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(NSInteger)month
{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMonth:month];
  //  [comps setDay:-1];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
    return mDate;
}

如果需要计算的日期的前一天 把注释的那一行代码打开就好

然后是计算N个工作日之后日期的方法

我这里只写了距离当前时间N个工作日之后的方法 如果需要自定义的事件 可以自己修改下

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSString *current = [self getCurrentTimes];
    NSString *time = [self stringFromDate:[self nextWeekdays:3]];
    NSLog(@"%@  %@",current,time);
}

//获取当前日期之后的N个工作日后的日期
- (NSDate *)nextWeekdays:(NSInteger)days{
    if (days <= 0) {
        return [NSDate date];
    }
    NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    
    NSInteger unitFlags = NSCalendarUnitWeekday;
    NSDate *today = [NSDate date];
    comps = [calendar components:unitFlags fromDate:today];
    NSInteger weekday = [comps weekday];
    NSInteger m = (days/5) * 7;
    NSInteger remainder = days % 5;
        //比如day 9  m==1  remainder == 4   week == 4
    switch (weekday) {
        case 1:
            m = ((days- 1) / 5) * 7 + remainder;//周天
            break;
        case 2:case 3:case 4:case 5:case 6:case 7:
        {
            NSInteger i = remainder + weekday - 2;
            NSInteger num = i / 5;
            NSInteger count = num * 2;
            NSLog(@"i=== %ld   num === %ld",(long)i,(long)num);
            m += remainder + count;

        }
        default:
            break;
    }
    return [self nextDay:m today:today];
}

- (NSDate *)nextDay:(NSInteger)day today:(NSDate *)today{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *component = [[NSDateComponents alloc] init];
    [component setDay:day];
    return [calendar dateByAddingComponents:component toDate:today options:0];
}

- (NSString *)stringFromDate:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *currentDateString = [dateFormatter stringFromDate:date];;
    return currentDateString;
}


//获取当前时间
-(NSString*)getCurrentTimes{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSDate *datenow = [NSDate date];
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    return currentTimeString;
}


你可能感兴趣的:(iOS计算N个工作日之后的日期和计算N个月之后的日期)