代码功能:获取当前月的前三个月 及后一个月,总共五个月
- (void)getDateData {
for (int i = 0; i < 5; i++) {
NSDateComponents * components = [[NSDateComponents alloc] init];
[components setMonth:(i-3)];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *nextData = [calendar dateByAddingComponents:components toDate:[NSDate date] options:NSCalendarMatchStrictly];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy.MM";
NSString * str1 = [formatter stringFrom![4E7B95DF-84E6-499C-8F7E-FA1970265118.png](http://upload-images.jianshu.io/upload_images/1787970-74484628a5b59b74.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Date:nextData];
[_pointArray addObject:str1];
}
}
setMonth:值为负数,则是前n个月,若为正数,则是后n个月。还可以获取年、日、时、分、秒