zoukankan      html  css  js  c++  java
  • 根据选择的日期自动计算天数

    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;

    }

  • 相关阅读:
    CentOS FTP基于虚拟用户的配置
    CentOS6.5 FTP配置
    MAC 隐藏文件的显示
    nignx知识点总结
    什么是函数式编程
    javascript有用代码片段
    PHP百杂
    好文:javascript中的四种循环
    好文:node.js最佳实践
    好文:不使用匿名函数的三个理由
  • 原文地址:https://www.cnblogs.com/Gaiayueyue/p/8258803.html
Copyright © 2011-2022 走看看