IOS获取指定年月的当月天数
程序员文章站
2023-12-13 09:07:40
前言
在开发ios中常常需要用到这一功能,在限定一个月的时间间隔为第一天和最后一天,需要知道这个月有多少天,才能知道最后一天是多少号,而且还要知道是否是闰年,可能2月只有...
前言
在开发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能有所帮助。