- (NSArray *)getAllWeekDate
{
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components: NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear fromDate:nowDate];
// 获取今天是周几 (周一是2,周日是1)
NSInteger weekDay = [comp weekday];
//这周是今年的第几周(注意:第一周编号为1不为0)
NSInteger weekOfYear = [comp weekOfYear];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMdd"];
NSDate *date = [formatter dateFromString:[formatter stringFromDate:[NSDate date]]];
//这周的开始和结束时间 (注意:+1 -1 避开临界值)
NSDate *startdate = [date dateByAddingTimeInterval:24*3600*(2 - weekDay)+1];
NSDate *enddate = [date dateByAddingTimeInterval:24*3600*(9 - weekDay)-1];
NSMutableArray *dateArray = [[[NSMutableArray alloc] init] mutableCopy];
//一年52周 获取一年的所有周一和周日的日期(注意:第一周编号为1不为0)
for (int i = 1; i <= 52; i++) {
NSDate *startdate1 = [startdate dateByAddingTimeInterval:24*3600*7*(i-weekOfYear)];
NSDate *enddate1 = [enddate dateByAddingTimeInterval:24*3600*7*(i-weekOfYear)];
[dateArray addObject:@[startdate1,enddate1]];
}
return dateArray;
}
嘿嘿嘿,写下来,再用的时候不用写了。
网友评论