iphone – 如何根据当前日期获取接下来的15天日期?

目前,我可以使用以下代码获取iPhone的当前日期

NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString* currentDateStr = [formatter stringFromDate:date];
NSLog(@"User's current Date:%@",currentDateStr);

但我希望从当前日期开始接下来的15天,我怎么能得到它?

最佳答案 试试这个:

NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDateComponents* components = [[[NSDateComponents alloc] init] autorelease];
components.day = 15;
NSDate* newDate = [calendar dateByAddingComponents: components toDate: self.date options: 0];
点赞