IOS获取指定年月的当月天数的方法
来源:爱站网时间:2020-11-24编辑:网友分享
我们在开发iOS的时候经常会需要获取指定年月,那么你知道iOS中如何获取指定年月吗?接下来我们就跟爱站小编一起去看看IOS获取指定年月的当月天数的方法。
我们在开发iOS的时候经常会需要获取指定年月,那么你知道iOS中如何获取指定年月吗?接下来我们就跟爱站小编一起去看看IOS获取指定年月的当月天数的方法。
前言
在开发IOS中常常需要用到这一功能,在限定一个月的时间间隔为第一天和最后一天,需要知道这个月有多少天,才能知道最后一天是多少号,而且还要知道是否是闰年,可能2月只有28天。
话不多说,附上代码:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSLog(@"%ld",(long)[self howManyDaysInThisYear:2016 withMonth:1]); NSLog(@"%ld",(long)[self howManyDaysInThisYear:2016 withMonth:2]); NSLog(@"%ld",(long)[self howManyDaysInThisYear:2016 withMonth:3]); NSLog(@"%ld",(long)[self howManyDaysInThisYear:2016 withMonth:4]); NSLog(@"%ld",(long)[self howManyDaysInThisYear:2016 withMonth:5]); NSLog(@"%ld",(long)[self howManyDaysInThisYear:2016 withMonth:6]); NSLog(@"%ld",(long)[self howManyDaysInThisYear:2016 withMonth:7]); NSLog(@"%ld",(long)[self howManyDaysInThisYear:2016 withMonth:8]); } #pragma mark - 获取某年某月的天数 - (NSInteger)howManyDaysInThisYear:(NSInteger)year withMonth:(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; }
以上内容就是小编介绍IOS获取指定年月的当月天数的方法,这篇文章以实例为主为,方便有经验的朋友,如果没有相关经验可以关注爱站技术频道。