//得到当前的时间
NSDate * mydate = [NSDate date];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"---当前的时间的字符串 =%@",currentDateStr);
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = nil;
comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitMonth fromDate:mydate];
NSDateComponents *adcomps = [[NSDateComponents alloc] init];
[adcomps setYear:0];
[adcomps setMonth:-2];
[adcomps setDay:0];
NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:mydate options:0]; NSString *beforDate = [dateFormatter stringFromDate:newdate];
NSLog(@"---前两个月 =%@",beforDate);
网友评论