解决方法
您将需要使用NSCalendar和NSDateComponents来执行您所追求的目标.像下面这样的东西应该做的伎俩.
NSDate *todaysDate = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; [dateComponents setYear:1]; NSDate *targetDate = [gregorian dateByAddingComponents:dateComponents toDate:todaysDate options:0]; [dateComponents release]; [gregorian release];
然后,以指定的格式将目标日期输出为字符串,您可以执行以下操作:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; Nsstring* dateString = [dateFormatter stringFromDate:targetDate];
希望这可以帮助.