一言不合,直接上代码:
1.根据当天的日期,推算出本周是从几号到几号
// 本天
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSString *Today = [dateFormatter stringFromDate:date];
// 本周的起止日期NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit|NSDayCalendarUnit
fromDate:now];
// 得到星期几
// 1(星期天) 2(星期一) 3(星期二) 4(星期三) 5(星期四) 6(星期五) 7(星期六)
NSInteger weekDay = [comp weekday];
// 得到几号
NSInteger day = [comp day];
DRNSLog(@"weekDay:%ld day:%ld",weekDay,day);
// 计算当前日期和这周的星期一和星期天差的天数
long firstDiff,lastDiff;
if (weekDay == 1) {
firstDiff = 1;
lastDiff = 0;
}else{
DRNSLog(@"%ld",[calendar firstWeekday]);
firstDiff = [calendar firstWeekday] - weekDay + 1;
lastDiff = 9 - weekDay - 1;
}
DRNSLog(@"firstDiff:%ld lastDiff:%ld",firstDiff,lastDiff);
// 在当前日期(去掉了时分秒)基础上加上差的天数
NSDateComponents *firstDayComp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[firstDayComp setDay:day + firstDiff];
NSDate *firstDayOfWeek= [calendar dateFromComponents:firstDayComp];
NSDateComponents *lastDayComp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[lastDayComp setDay:day + lastDiff];
NSDate *lastDayOfWeek= [calendar dateFromComponents:lastDayComp];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
// [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[formater setDateFormat:@"yyyy-MM-dd"];
DRNSLog(@"星期一开始 %@",[formater stringFromDate:firstDayOfWeek]);
DRNSLog(@"当前 %@",[formater stringFromDate:now]);
DRNSLog(@"星期天结束 %@",[formater stringFromDate:lastDayOfWeek]);
//2.推算出本月的起止日期
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit|NSDayCalendarUnit
fromDate:now];
// 得到星期几
// 1(星期天) 2(星期一) 3(星期二) 4(星期三) 5(星期四) 6(星期五) 7(星期六)
// NSInteger weekDay = [comp weekday];
// 得到几号
NSInteger day = [comp day];
NSInteger month = [comp month];
DRNSLog(@"month:%ld day:%ld",month,day);
NSDateComponents *firstDayComp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[firstDayComp setDay:1];
NSDate *firstDayOfWeek= [calendar dateFromComponents:firstDayComp];
NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:now];
NSDateComponents *lastDayComp = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[lastDayComp setDay:range.length];
NSDate *lastDayOfWeek= [calendar dateFromComponents:lastDayComp];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
// [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[formater setDateFormat:@"yyyy-MM-dd"];
startdate = [formater stringFromDate:firstDayOfWeek];
enddate = [formater stringFromDate:lastDayOfWeek];
[self loadData1];