我有两个文本字段,如果我在第一个文本字段中输入日期...我应该在第二个字段中参考该日期获得正好一周的日期。我在NSDate中搜索但是无法获取它
请提出任何建议
提前致谢:)
答案 0 :(得分:4)
以下代码可以帮助您:
NSDate *now = [NSDate date];
int daysToAdd = 50; // or 60 :-)
// set up date components
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:daysToAdd];
// create a calendar
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:now options:0];
NSLog(@"Clean: %@", newDate2);
//for displaying date in text field
txt.text = [NSString stringWithFormat:@"%@",newDate2];
答案 1 :(得分:1)
NSDate *newDate = [startDate dateByAddingTimeInterval:(86400 * 4)];