年月周日

NSCalendar 功能很强大,多次调用时需要考虑性能问题,但必要的使用会在日期计算时事半功倍。

计算周次所在日期范围
-(NSString*)timeConversionYear:(NSInteger)year WeakOfYear:(NSInteger)weekofYear
{
    //周次的范围日期  几月几日 - 几月几日
    NSString *weekDate = @"";
    //时间轴 取每一年的六月一号 没有特别的含义
    NSString *timeAxis = [NSString stringWithFormat:@"%ld-06-01 12:00:00",(long)year];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //获得了时间轴
    NSDate *date = [dateFormatter dateFromString:timeAxis];
    
    //日历类 提供大部分的时间计算接口
    NSCalendar *calendar = [NSCalendar currentCalendar];
    /**这两个参数的设置影响着周次的个数和划分*****************/
    [calendar setFirstWeekday:2]; //设置每周的开始是星期一
    [calendar setMinimumDaysInFirstWeek:7]; //设置一周至少需要几天
    /****************/
    //一个封装了具体年月日、时秒分、周、季度等的类
    NSDateComponents *comps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                          fromDate:date];
    
    //时间轴是当前年的第几周
    NSInteger todayIsWeek = [comps weekOfYear];
    //第几周的字符串格式
    //NSString *todayIsWeekStr = [NSString stringWithFormat:@"%ld",(long)todayIsWeek];
    
    //获取时间轴是星期几 1(星期天) 2(星期一) 3(星期二) 4(星期三) 5(星期四) 6(星期五) 7(星期六)
    NSInteger todayIsWeekDay = [comps weekday];
    
    //得到时间轴是几号
    // NSInteger todayIsDay = [comps day];
    // 计算当前日期和这周的星期一和星期天差的天数
    //firstDiff 星期一相差天数 、 lastDiff 星期天相差天数
    long firstDiff,lastDiff;
    if (todayIsWeekDay == 1) {
        firstDiff = -6;
        lastDiff = 0;
    }else
    {
        firstDiff = [calendar firstWeekday] - todayIsWeekDay;
        lastDiff = 8 - todayIsWeekDay;
    }
    
    NSDate *firstDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*firstDiff sinceDate:date];
    NSDate *lastDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*lastDiff sinceDate:date];
    
    long weekdifference = weekofYear - todayIsWeek;
    
    firstDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*7*weekdifference sinceDate:firstDayOfWeek];
    lastDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*7*weekdifference sinceDate:lastDayOfWeek];
    // NSLog(@"星期一的日期 %@",[dateFormatter stringFromDate:firstDayOfWeek]);
    // NSLog(@"星期天的日期 %@",[dateFormatter stringFromDate:lastDayOfWeek]);
    
    NSDateComponents *firstDayOfWeekcomps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                                        fromDate:firstDayOfWeek];
    NSDateComponents *lastDayOfWeekcomps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                                       fromDate:lastDayOfWeek];
    
    NSInteger startMonth = [firstDayOfWeekcomps month];
    NSInteger startDay = [firstDayOfWeekcomps day];
    
    NSInteger endmonth = [lastDayOfWeekcomps month];
    NSInteger endday = [lastDayOfWeekcomps day];
    weekDate = [NSString stringWithFormat:@"%ld.%ld.%ld-%ld.%ld.%ld",year,(long)startMonth,(long)startDay,year,(long)endmonth,(long)endday];
    return weekDate;
}
计算年有多少周
+(NSInteger)weeksInOneYear:(NSInteger)year
{
    NSString *timeAxis = [NSString stringWithFormat:@"%ld-12-31 12:00:00",(long)year];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //获得了时间轴
    NSDate *date = [dateFormatter dateFromString:timeAxis];
    //日历类 提供大部分的时间计算接口
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:2];
    [calendar setMinimumDaysInFirstWeek:7];
    
    //    NSRange range = [calendar rangeOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitYear forDate:date];
    NSInteger length = [calendar ordinalityOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitYear forDate:date];
    return length;
}

你可能感兴趣的:(年月周日)