iOS开发 得到当前时间前第几年第几月第几天的日期
2022-09-29 本文已影响0人
那月无痕
//得到当前的时间
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);