Iphone 相对于在第一个字段中输入的日期,在第二个字段中自动显示一周后的日期
我有两个文本字段,如果我在第一个文本字段中输入日期…我应该在第二个字段中根据该日期恰好获得一周的日期。我在NSDate中搜索了该日期,但无法获得它 有什么建议吗Iphone 相对于在第一个字段中输入的日期,在第二个字段中自动显示一周后的日期,iphone,ios,xcode,datepicker,nsdate,Iphone,Ios,Xcode,Datepicker,Nsdate,我有两个文本字段,如果我在第一个文本字段中输入日期…我应该在第二个字段中根据该日期恰好获得一周的日期。我在NSDate中搜索了该日期,但无法获得它 有什么建议吗 提前感谢:)可能以下代码将帮助您: NSDate *now = [NSDate date]; int daysToAdd = 50; // or 60 :-) // set up date components NSDateComponents *components = [[[NSDateCompon
提前感谢:)可能以下代码将帮助您:
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];
以下代码可能对您有所帮助:
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];
非常感谢您的回答,我们将实施它并返回给您:)不。不要使用像这样的硬编码时间。这在某些情况下会中断。非常感谢您的回答,我们将实施它并返回给您:)不。不要使用像这样的硬编码时间。这在某些情况下会中断。感谢您的回答,让我实施并返回给您:)是的,斯塔克先生,逻辑工作完美,它正在控制台中显示,但不是在必填字段中,请建议我应该做什么。。。提前感谢:)嘿,Eshwar Chaitanya,我已经根据您的评论编辑了我的答案…请查看。感谢您的答案,让我实施并返回给您:)是的,斯塔克先生,逻辑工作完美,它正在控制台中显示,但不是在必填字段中,请建议我该怎么做。。。提前感谢:)嘿,埃什瓦尔·柴坦尼亚,我已经根据你的评论编辑了我的答案……请查看。