//得到当前的时间
NSDate * mydate = [NSDate date];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
//date转化为string
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [[NSDateComponents alloc] init];
//正值加日期、负值减日期
[comps setYear:0];
[comps setMonth:-2];
[comps setDay:0];
NSDate *newdate = [calendar dateByAddingComponents:comps toDate:mydate options:0];
NSString *beforDate = [dateFormatter stringFromDate:newdate];
网友评论