当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS获取指定年月的当月天数

IOS获取指定年月的当月天数

2019年07月24日  | 移动技术网IT编程  | 我要评论

武夷山凤凰红茶,绿色椅子下载地址,棺修

前言

在开发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能有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网