近日 根据公司项目需求折腾掉了半条命呀
直接上图
大致解释一下功能的需求:
展示用户的练习历史数据,首先先说明一下服务器的数据返回结构和逻辑,就拿“周”来说吧,本地会每次分页请求十条数据,服务器便返回有数据的十条周次的数据给我,比如我查询去年一年的练习数据,数据格式是这样的:
这是周次的格式
{
exerciseDuration = 0;
studyDuration = 2;
subjectNum = 0;
teachDuration = 0;
totalDuration = 2;
week = 01; //周次
year = 2017; //年份
}
月的格式相信大家很了解了吧
{
exerciseDuration = 0;
month = 10; //月份
studyDuration = 23;
subjectNum = 0;
teachDuration = 0;
totalDuration = 23;
year = 2016; //年份
}
上代码:计算week对应的日期
{
NSArray *dataArr = [PractiseWeekMonthModel mj_objectArrayWithKeyValuesArray:data];//将服务器请求的数据转换成模型数组
if (dataArr.count == 0) {
return;
}
for (int i = 0; i < dataArr.count; i++) { //遍历数组
PractiseWeekMonthModel * model = (PractiseWeekMonthModel *)[dataArr objectAtIndex:i];//单个数据模型
int year = (int)[model.year integerValue]; //模型中记录的年份
int weekofyear = (int)[model.week integerValue]; //模型中记录的年份
/**
* @property (nonatomic, assign) int currentYear;
* @property (nonatomic, assign) int currentMonth;
* @property (nonatomic, assign) int currentWeek;
* @property (nonatomic, strong) NSDate *currentDate;
*
* NSDate *currentDate = [NSDate date];
* NSCalendar *currentcalendar = [NSCalendar currentCalendar];
* [currentcalendar setFirstWeekday:2]; //设置每周的开始是星期一
* [currentcalendar setMinimumDaysInFirstWeek:7];
* NSDateComponents *currentcomps = [currentcalendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
fromDate:currentDate];
*self.currentYear = (int)[currentcomps year];
* self.currentMonth = (int)[currentcomps month];
* self.currentWeek = (int)[currentcomps weekOfYear];
* NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
* [dateFormatter setDateFormat:@"yyyy-MM-dd"];
* NSString *currentTime = [dateFormatter stringFromDate:currentDate];
* NSDateFormatter *currentdateFormatter = [[NSDateFormatter alloc] init];
* [currentdateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
* self.currentDate = [currentdateFormatter dateFromString:[NSString stringWithFormat:@"%@ 12:00:00",currentTime]];
*/
for (int y = self.currentYear; y >= year; y--) { //将获取的年份与当前记录的年份比较
int calculationWeek = weekofyear; //获得周次
if (y > year) {
calculationWeek = 1;
}
for (int w = self.currentWeek; w >= calculationWeek; w--) {
if (w == calculationWeek && y == year) {
model.dateName = [NSObject timeConversionYear:y WeakOfYear:w];
[metaDataArr addObject:model];
}
else
{
PractiseWeekMonthModel * model = [PractiseWeekMonthModel alloc];
model.totalDuration = 0;
model.dateName = [NSObject timeConversionYear:y WeakOfYear:w];;
[metaDataArr addObject:model];
}
}
if (y > year) {
self.currentWeek = (int)[NSObject weeksInOneYear:y];
}
else
{
self.currentWeek = calculationWeek - 1;
}
}
self.currentYear = year;
}
//记录数组中的数据个数
NSInteger Initial = self.practiseHistroyArr.count;
self.practiseHistroyArr = (NSMutableArray *)[[self.practiseHistroyArr reverseObjectEnumerator] allObjects];
[self.practiseHistroyArr addObjectsFromArray:metaDataArr];
if (Initial > 0) {
self.selection = self.practiseHistroyArr.count - Initial;
}
else
{
self.selection = self.practiseHistroyArr.count - 1;
}
self.practiseHistroyArr = (NSMutableArray *)[[self.practiseHistroyArr reverseObjectEnumerator] allObjects];
}
//
+(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;
}
/**根据周次获取周次的范围日期*/
+(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",(long)startMonth,(long)startDay,(long)endmonth,(long)endday];
return weekDate;
}