2011-05-10 56 views

回答

11

根据苹果文档的NSDate的,还有一类方法:

+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)seconds 

60秒/分钟* 60分钟/小时* 24小时/天* 30天应该给你所需的秒数。

所以尝试:

NSDate *futureDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * 30]; 
+0

我想过去的约会.....如何做到这一点的任何想法? – 2012-08-28 13:44:13

+1

有人希望你谨慎对待风,并发现你可以使用负NSTimeInterval来获取过去的日期/时间。 – mharper 2012-08-30 19:59:01

8

如下你可以这样做:

NSDate *now = [NSDate date]; 

    // now a NSDate object for now + 30 days 
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 


    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; 
    [offsetComponents setDay:30]; 
    NSDate *endDate = [gregorian dateByAddingComponents:offsetComponents toDate:now options:0]; 
    [offsetComponents release]; 

    [gregorian release]; 
+0

如果你想同时添加不同成分这也适用:你可以添加天,月,年,小时,分钟和秒! – 2011-05-10 18:33:30