在一周后显示在第一个字段中输入日期的第二个字段中的日期

问题描述:

我有两个文本字段,如果我在第一个文本字段中输入一个日期...我应该得到的日期与参考完全一周到那个日期在第二个领域。我在NSDate搜索,但无法得到它。在一周后显示在第一个字段中输入日期的第二个字段中的日期

任何建议,请提前:)

感谢

可能下面的代码将帮助您:

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]; 
+0

感谢您的回答,让我实现并回复你: ) – 2011-12-28 11:30:35

+0

是的Mr.Stark,逻辑工作完美,它显示在控制台中,但没有在必填字段中,请建议我该怎么做... 在此先感谢:) – 2011-12-28 12:23:37

+0

嘿Eshwar Chaitanya我已根据您的评论编辑我的答案...请检查出来。 – Stark 2011-12-28 13:16:24

NSDate *newDate = [startDate dateByAddingTimeInterval:(86400 * 4)]; 
+0

非常感谢您的答案,将实施它,并得到回复:) – 2011-12-28 11:30:09

+1

否不要使用这样的硬编码时间。这将在某些条件下破坏。 – 2012-01-27 17:53:08