传入今日日期及距今的月份month:
- (NSDate*)getPriousorLaterDateFromDate:(NSDate*)date withMonth:(int)month{
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setMonth:month];
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];// NSGregorianCalendar
NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
return mDate;
}
eg:距今三个月前:
//获取当前时间,日期
NSDate *currentDate = [NSDate date];
//获取前三个月的时间
NSDate *monthagoData = [GlobelConfig getPriousorLaterDateFromDate:currentDate withMonth:-3];
网友评论