媒介
在開辟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獲得指定年代確當月天數的全體內容,願望本文的內容對年夜家開辟IOS能有所贊助。
【IOS獲得指定年代確當月天數】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!