【问题标题】:Displaying after one week Date in second field automatically with respect to the date entered in first field一周后显示第二个字段中的日期相对于第一个字段中输入的日期
【发布时间】:2011-12-28 11:17:19
【问题描述】:

我有两个文本字段,如果我在第一个文本字段中输入日期...我应该在第二个字段中参考该日期获得恰好一周的日期。我在 NSDate 中搜索但无法获得它.

有什么建议

提前致谢:)

【问题讨论】:

    标签: iphone ios xcode datepicker nsdate


    【解决方案1】:

    下面的代码可以帮助你:

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

    【讨论】:

    • 感谢您的回答,让我实施并回复您:)
    • 是的,Stark 先生,逻辑工作完美,它正在控制台中显示,但不是在必填字段中,请建议我应该怎么做...提前谢谢:)
    • 嘿,Eshwar Chaitanya,我已根据您的评论编辑了我的答案...请查看。
    【解决方案2】:
    NSDate *newDate = [startDate dateByAddingTimeInterval:(86400 * 4)];
    

    【讨论】:

    • 非常感谢您的回答,将实施并回复您:)
    • 没有。不要使用这样的硬编码时间。这会在某些情况下中断。
    猜你喜欢
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多