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;

}

版权声明:本文为Gaiayueyue原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Gaiayueyue/p/8258803.html