iOS 获取当前月份的天数(转)、

转:http://www.cnblogs.com/kingbo/p/3966855.html

1. 获取当前月份有多少天
NSCalendar *calendar = [NSCalendarcurrentCalendar];

 

NSRange range = [calendarrangeOfUnit:NSDayCalendarUnitinUnit:NSMonthCalendarUnitforDate:[NSDatedate]];

 

NSUInteger numberOfDaysInMonth = range.length;

 

 

numberOfDaysInMonth 即为当月的天数。更多用法查看NSCalendar, NSDateComponents帮助文档

 

 

 

2. 获取当前年份和月份和天数

 

NSCalendar *calendar = [NSCalendar currentCalendar];

 

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;

 

NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];

 

NSInteger iCurYear = [components year];  //当前的年份

 

NSInteger iCurMonth = [components month];  //当前的月份

 

NSInteger iCurDay = [components day];  // 当前的号数

 

posted @ 2015-04-01 17:18  爱生活爱代码  阅读(524)  评论(0编辑  收藏  举报