zoukankan      html  css  js  c++  java
  • iOS 获取本周,本月,本季,的起止日期

    - (void)getQuarterBeginAndEndWith:(NSDate *)currentDate{

        NSDate *nowDate = [NSDate date];

        NSCalendar *calendar = [NSCalendar currentCalendar];

        NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday  fromDate:nowDate];

        /**获取当前的月份*/

        NSInteger curentMonth = [comp month];

        NSString * currentYear = [NSString stringWithFormat:@"%ld",[comp year]];

        NSString * beginTime = [self getQuarterStartDateWithYear:currentYear currentMonth:curentMonth];

        NSString * endTime = [self getQuarterEndDateWithYear:currentYear currentMonth:curentMonth];

        NSString * str = [NSString stringWithFormat:@"季度开始时间 == %@  季度结束时间 == %@",beginTime,endTime];

        NSLog(@"%@",str);

    }

     

    //获得某季度的开始日期

    - (NSString *)getQuarterStartDateWithYear:(NSString *)paraYear currentMonth:(NSInteger)currentMonth {

        

        NSString * beginTime = @"";

        if (currentMonth < 4) {

            beginTime = [paraYear stringByAppendingString:@"-01-01"];

        } else if (currentMonth >= 4 && currentMonth < 7) {

            beginTime = [paraYear stringByAppendingString:@"-04-01"];

        } else if (currentMonth >= 7 && currentMonth < 10) {

            beginTime = [paraYear stringByAppendingString:@"-07-01"];

        } else if (currentMonth >= 10 && currentMonth < 12) {

            beginTime = [paraYear stringByAppendingString:@"-10-01"];

        }

        return beginTime;

    }

     

    //获得某季度的结束日期

    - (NSString *)getQuarterEndDateWithYear:(NSString *)paraYear currentMonth:(NSInteger)currentMonth {

        NSString * endTime = @"";

        if (currentMonth < 4) {

            endTime = [paraYear stringByAppendingString:@"-03-31"];

        } else if (currentMonth >= 4 && currentMonth < 7) {

            endTime = [paraYear stringByAppendingString:@"-06-30"];

        } else if (currentMonth >= 7 && currentMonth < 10) {

            endTime = [paraYear stringByAppendingString:@"-09-30"];

        } else if (currentMonth >= 10 && currentMonth < 12) {

            endTime = [paraYear stringByAppendingString:@"-12-31"];

        }

        return endTime;

    }

     

     

    - (void)getWeekBeginAndEndWith:(NSDate *)newDate{

        NSDate *nowDate = [NSDate date];

        NSCalendar *calendar = [NSCalendar currentCalendar];

        NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday  fromDate:nowDate];

        // 获取今天是周几

        NSInteger weekDay = [comp weekday];

        /**获取当前几个月*/

        NSInteger monthDay = [comp month];

        NSLog(@"%ld",monthDay);

        

        // 获取几天是几号

        NSInteger day = [comp day];

        NSLog(@"%ld----%ld",weekDay,day);

        

        // 计算当前日期和本周的星期一和星期天相差天数

        long firstDiff,lastDiff;

        //    weekDay = 1;

        if (weekDay == 1)

        {

            firstDiff = -6;

            lastDiff = 0;

        }

        else

        {

            firstDiff = [calendar firstWeekday] - weekDay + 1;

            lastDiff = 8 - weekDay;

        }

        NSLog(@"firstDiff: %ld   lastDiff: %ld",firstDiff,lastDiff);

        

        // 在当前日期(去掉时分秒)基础上加上差的天数

        NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:nowDate];

        [firstDayComp setDay:day + firstDiff];

        NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];

        

        NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay   fromDate:nowDate];

        [lastDayComp setDay:day + lastDiff];

        NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];

        

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

        [formatter setDateFormat:@"MM月dd日"];

        NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];

        NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];

        NSLog(@"%@=======%@",firstDay,lastDay);

        

        NSString *dateStr = [NSString stringWithFormat:@"%@-%@",firstDay,lastDay];

        

        NSLog(@"%@",dateStr);

    }

     

    - (void)getMonthBeginAndEndWith:(NSDate *)newDate{

        if (newDate == nil) {

            newDate = [NSDate date];

            

        }

        double interval = 0;

        NSDate *beginDate = nil;

        NSDate *endDate = nil;

        NSCalendar *calendar = [NSCalendar currentCalendar];

        [calendar setFirstWeekday:2];

        //设定周一为周首日

        BOOL ok = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:newDate]; //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit

        if (ok) {

            endDate = [beginDate dateByAddingTimeInterval:interval-1];

            

        }else {

            return;

            

        }

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

        [myDateFormatter setDateFormat:@"yyyy.MM.dd"];

        NSString *beginString = [myDateFormatter stringFromDate:beginDate];

        NSString *endString = [myDateFormatter stringFromDate:endDate];

        NSString *s = [NSString stringWithFormat:@"%@-%@",beginString,endString];

        NSLog(@"%@",s);

        

    }

        

  • 相关阅读:
    HihoCoder1105 题外话·堆(基础二叉搜索树)
    HihoCoder1080 更为复杂的买卖房屋姿势(线段树+多重lazy)
    HihoCoder1078线段树的区间修改(线段树+lazy)
    HihoCoder1070 区间最小值(简单线段树)
    HDU3577Fast Arrangement(线段树+lazy)
    POJ2985 The k-th Largest Group (并查集+treap)
    HihoCoder1337 动态第k大(treap)
    STL的erase函数和lower_bound
    HihoCoder1329 平衡树·Splay(附STL版)
    etcd 在超大规模数据场景下的性能优化
  • 原文地址:https://www.cnblogs.com/Jackie-subDai/p/9552540.html
Copyright © 2011-2022 走看看