NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM"];
NSString *currentTime2 = [formatter stringFromDate:[NSDate date]];
NSArray * arr =[self getMonthFirstAndLastDayWith:currentTime2];
//方法
- (NSArray *)getMonthFirstAndLastDayWith:(NSString *)dateStr{
NSDateFormatter *format=[[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM"];
NSDate *newDate=[format dateFromString:dateStr];
double interval = 0;
NSDate *firstDate = nil;
NSDate *lastDate = nil;
NSCalendar *calendar = [NSCalendar currentCalendar];
BOOL OK = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:newDate];
if (OK) {
lastDate = [firstDate dateByAddingTimeInterval:interval - 1];
}else {
return @[@"",@""];
}
NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *firstString = [myDateFormatter stringFromDate: firstDate];
NSString *lastString = [myDateFormatter stringFromDate: lastDate];
return @[firstString, lastString];
}
网友评论