NSArray *mouthArr = [[self getFirstAndLastdayinWeek] componentsSeparatedByString:@"-"];
NSArray *weekFArr = [mouthArr[0] componentsSeparatedByString:@"."];
NSArray *weekLArr = [mouthArr[1] componentsSeparatedByString:@"."];
int a = 0;
int b = 0;
a= [weekFArr[1] intValue];//14
b= [weekLArr[1] intValue];//20
_NumD = 1;
NSInteger weekNumInMonth = [self getWeekdayOrdinalInMonth];//5;
NSInteger weekNum = [self getlinesNumInMonth:2017 month:8];
NSMutableArray *muarray=[[NSMutableArray alloc]init];
for(int i = 1;i<=weekNum;i++){
[muarray addObject:[NSString stringWithFormat:@"%d",i]];
}
if (weekNumInMonth == weekNum) {
for (int i = 1; i<= weekNum ; i++ ) {
DLog(@"self getFirstAndLastdayinWeek:%@",[self getFirstAndLastdayinWeek]);
DLog(@"%d",i);
muarray [weekNum - i] = [self getFirstAndLastdayinWeek];
_NumD ++;
}
}else if(weekNumInMonth < weekNum) {
for (int i = 1; i<= weekNumInMonth - 1; i++ ) {
_NumD ++;
muarray [weekNumInMonth - 1 - i] = [self getFirstAndLastdayinWeek];
DLog(@"self getFirstAndLastdayinWeek>>:%@",[self getFirstAndLastdayinWeek]);
}
_NumD =1;
for (int i = 0; i< weekNum - weekNumInMonth + 1; i++ ) {
DLog(@"self getFirstAndLastdayinWeek000000:%@",[self getFirstAndLastdayinWeek]);
muarray [weekNumInMonth + i -1] = [self getFirstAndLastdayinWeek];
_NumD --;
}
}
DLog(@"muarray:%@",muarray);
#pragma mark - 获取当前周的星期一和星期日
- (NSString *)getFirstAndLastdayinWeek
{
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay |NSCalendarUnitWeekdayOrdinal fromDate:nowDate];
// 获取今天是周几
NSInteger weekDay = [comp weekday];
// 获取几天是几号
NSInteger day = [comp day];
NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
// 计算周一
long firstDiff,lastDiff;
//weekDay == 1 星期日
if (weekDay == 1)
{
firstDiff = -6;
lastDiff = 0;
if(_NumD > 1)
{
firstDiff = [calendar firstWeekday] - weekDay + 1 - ((_NumD)*7);
lastDiff = 8 - weekDay;
}
}
else
{
if(_NumD == 0)
{
firstDiff = [calendar firstWeekday] - weekDay + 1;
lastDiff = 8 - weekDay;
[firstDayComp setDay:day + firstDiff + weekDay];
}
else
{
firstDiff = [calendar firstWeekday] - weekDay + 1 - ((_NumD -1)*7);
lastDiff = 8 - weekDay;
[firstDayComp setDay:day + firstDiff];
}
}
NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
NSDateFormatter *firstDayformatter = [[NSDateFormatter alloc] init];
[firstDayformatter setDateFormat:@"M.d"];
// 本周第一天时间
NSString *firstDay = [firstDayformatter stringFromDate:firstDayOfWeek];
// 计算周日
long firstDiffs,lastDiffs;
if (weekDay == 1)
{
firstDiffs = -6;
lastDiffs = 0;
if(_NumD > 1)
{
firstDiffs = [calendar firstWeekday] - weekDay + 1 - (_NumD *7);
lastDiffs = 8 - weekDay - (_NumD *7);
}
}
else
{
if(_NumD == 1)
{
firstDiffs = [calendar firstWeekday] - weekDay + 1;
lastDiffs = 8 - weekDay;
}else
{
firstDiffs = [calendar firstWeekday] - weekDay + 1 - ((_NumD -1)*7);
lastDiffs = 8 - weekDay - ((_NumD -1)*7);
}
}
NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
[lastDayComp setDay:day + lastDiffs];
NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];
NSDateFormatter *lastDayformatter = [[NSDateFormatter alloc] init];
[lastDayformatter setDateFormat:@"M.d"];
// 本周最后一天
NSString *lastDay = [lastDayformatter stringFromDate:lastDayOfWeek];
NSString *firstlastDay = [NSString stringWithFormat:@"%@-%@",firstDay,lastDay];
return firstlastDay;
}
#pragma mark - 获取某月有几个星期
-(NSInteger)getlinesNumInMonth:(int)year month:(int)imonth{
//根据年月计算这个月有多少天
self.days = [self numberOfDaysInYears:year month:imonth];
//获取第一天星期几
self.firsrtDay = [self GetTheWeekOfDayByYera:year andByMonth:imonth];
//第一周的天数 7 - self.firsrtDay
//占几行 占得满周 最后剩下的天数
self.linesNum = 1 + (self.days - ( 7 - self.firsrtDay )) / 7 +( ( self.days - ( 7 - self.firsrtDay ) )%7 > 0? + 1 : + 0 );
if (self.firsrtDay == 7)
self.linesNum -- ;
return self.linesNum;
}
- (NSUInteger)numberOfDaysInYears:(NSInteger )year month:(NSInteger )month{
if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
return 31 ;
if((month == 4) || (month == 6) || (month == 9) || (month == 11))
return 30;
if((year % 4 == 1) || (year % 4 == 2) || (year % 4 == 3))
{
return 28;
}
if(year % 400 == 0)
return 29;
if(year % 100 == 0)
return 28;
return 29;
}
#pragma mark - 设置日历 ----- 确定当前月的第一天星期几 -> 当前月份的第一周有几天 -
-(NSInteger)GetTheWeekOfDayByYera:(NSInteger )year andByMonth:(NSInteger )month{
NSInteger sum = 0;
for(NSInteger i = 1;i
网友评论