//调用
NSInteger i = [self howManyDaysInThisMonth:2];
NSLog(@"%ld",(long)i);
// 获取今年指定月的天数
- (NSInteger)howManyDaysInThisMonth :(NSInteger)imonth {
int year = [[self years][0] intValue];
if((imonth == 1)||(imonth == 3)||(imonth == 5)||(imonth == 7)||(imonth == 8)||(imonth == 10)||(imonth == 12))
return 31 ;
if((imonth == 4)||(imonth == 6)||(imonth == 9)||(imonth == 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 获取当前的年月
- (NSArray *) years {
NSMutableArray *yearArr = [[NSMutableArray alloc]init];
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth;
NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
NSInteger year = [dateComponent year];
NSInteger month = [dateComponent month];
[yearArr addObject:@(year)];
[yearArr addObject:@(month)];
return yearArr;
}