iOS获取一个月所有天数
2017-07-10 本文已影响22人
我的天空蔚蓝色
- (void) getAllDaysWithMonth
{
NSUInteger dayCount = [self getInMonthNumberOfDays]; //一个月的总天数
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
NSDate * currentDate = [NSDate date];
[formatter setDateFormat:@"yyyy-MM"];
NSString * str = [formatter stringFromDate:currentDate];
[formatter setDateFormat:@"yyyy-MM-dd"];
for (NSInteger i = 1; i <= dayCount; i++) {
NSString * sr = [NSString stringWithFormat:@"%@-%ld",str,i];
[self.dateTimeArray addObject:sr];
}
}```
// 获取当月的天数
- (NSInteger)getInMonthNumberOfDays
{
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate * currentDate = [NSDate date];
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
inUnit: NSCalendarUnitMonth
forDate:currentDate];
return range.length;
}```
(
"2017-07-1",
"2017-07-2",
"2017-07-3",
"2017-07-4",
"2017-07-5",
"2017-07-6",
"2017-07-7",
"2017-07-8",
"2017-07-9",
"2017-07-10",
"2017-07-11",
"2017-07-12",
"2017-07-13",
"2017-07-14",
"2017-07-15",
"2017-07-16",
"2017-07-17",
"2017-07-18",
"2017-07-19",
"2017-07-20",
"2017-07-21",
"2017-07-22",
"2017-07-23",
"2017-07-24",
"2017-07-25",
"2017-07-26",
"2017-07-27",
"2017-07-28",
"2017-07-29",
"2017-07-30",
"2017-07-31"
)