根据选择的日期自动计算天数

1、以12点为节点

+ (CGFloat)beginFKBTime:(NSString *)bTime endFKBTime:(NSString *)eTime{

    NSArray *bArray = [bTime componentsSeparatedByString:@" "];

    NSArray *eArray = [eTime componentsSeparatedByString:@" "];

    NSDateFormatter *Formatter=[[NSDateFormatter alloc] init];

    [Formatter setDateFormat:@"yyyy-MM-dd HH:mm"];

    [Formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    NSDate *bDate = [Formatter dateFromString:bTime];

    NSDate *eDate = [Formatter dateFromString:eTime];

    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    NSDate *bDate5 = [dateFormatter dateFromString:bArray.firstObject];

    NSDate *eDate5 = [dateFormatter dateFromString:eArray.firstObject];

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    unsigned int unitFlags = NSCalendarUnitDay;

    NSDateComponents *comps = [gregorian components:unitFlags fromDate:bDate5  toDate:eDate5  options:0];

    CGFloat time = 0;

    BOOL bTimeAF = NO;//是否上午出发

    BOOL eTimeAF = NO;//是否上午回来

    NSDate *bDate1 = [bDate earlierDate:[Formatter dateFromString:[NSString stringWithFormat:@"%@ 12:00",bArray.firstObject]]];

    NSDate *eDate1 = [eDate earlierDate:[Formatter dateFromString:[NSString stringWithFormat:@"%@ 12:00",eArray.firstObject]]];

    if ([comps day] == 0) {

        if ([bDate isEqualToDate:bDate1]) {

            bTimeAF = YES;

        }

        if ([eDate isEqualToDate:eDate1]) {

            eTimeAF = YES;

        }

        if (bTimeAF && eTimeAF) {//上午出发 上午回来

            time = 0.5;

        }else if (bTimeAF && eTimeAF == NO){//上午出发 下午回来

            time = 1.0;

        }else if (!bTimeAF && !eTimeAF){//下午出发 下午回来

            time = 0.5;

        }else if (bTimeAF == NO && eTimeAF){//下午出发 上午回来

            time = 0;

        }

    }else{

        if ([bDate isEqualToDate:bDate1]) {

            bTimeAF = YES;

        }

        if ([eDate isEqualToDate:eDate1]) {

            eTimeAF = YES;

        }

        if (bTimeAF && eTimeAF) {//上午出发 上午回来

            time = 0.5;

        }else if (bTimeAF && eTimeAF == NO){//上午出发 下午回来            

            time = 1.0;

        }else if (!bTimeAF && !eTimeAF){//下午出发 下午回来

            time = 0.5;

        }else if (bTimeAF == NO && eTimeAF){//下午出发 上午回来

            time = 0;

        }

    }

    return [comps day] + time;

}

2、正常的计算

+ (NSInteger)startDate:(NSDate *)startDate endDate:(NSDate *)endDate {

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents * comp = [calendar components:NSCalendarUnitDay

                                             fromDate:startDate

                                               toDate:endDate

                                              options:NSCalendarWrapComponents];

    return comp.day;

}

转载于:https://www.cnblogs.com/Gaiayueyue/p/8258803.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值