iOS 获取今年指定月的天数
Posted tongyuling
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 获取今年指定月的天数相关的知识,希望对你有一定的参考价值。
//调用
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;
}
以上是关于iOS 获取今年指定月的天数的主要内容,如果未能解决你的问题,请参考以下文章