iOS 获取本周,本月,本季,的起止日期

Posted 后羿射蛋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);

    

}

    

以上是关于iOS 获取本周,本月,本季,的起止日期的主要内容,如果未能解决你的问题,请参考以下文章

转: sql语句获取本周本月数据

sql语句获取本周本月本年数据

SQL Server 获取本周,本月,本年等时间内记录

SQL Server 获取本周,本月,本年等时间内记录

asp取access数据库里本周、本月、本季度的记录,select语句怎么写?

JS获取本周本季度本月上月的开始日期结束日期