NSDate获得今天后第30天的价值

Max*_*me 3 iphone objective-c

我需要在今天的第30天设置NSDate的值.我寻找一种快速的方法来做到这一点.

坦克!

mha*_*per 11

根据Apple Docs for NSDate,有一种类方法:

+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)seconds
Run Code Online (Sandbox Code Playgroud)

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

所以尝试:

NSDate *futureDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * 30];
Run Code Online (Sandbox Code Playgroud)


Mas*_*aro 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];
Run Code Online (Sandbox Code Playgroud)