2

我对 iOS 开发非常陌生。我想在一周后得到日期。

我使用代码来获取当前日期,如下所示。

NSDate *currentDateTime = [NSDate date];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"EEE,MM-dd-yyyy HH:mm:ss"];

NSString *dateInStringFormated = [dateFormatter stringFromDate:currentDateTime];

NSLog(@"%@", dateInStringFormated);

如果我的当前日期为 2013 年 3 月 12 日,我想在一周后获得日期意味着获得 2013 年 3 月 19 日。

4

3 回答 3

13

使用NSDateComponents

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [NSDateComponents new];
comps.day = 7;
NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
于 2013-03-12T05:54:38.773 回答
1
NSDate *todayDate = [NSDate date];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:+7];
NSDate *afterSevenDays = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:todayDate options:0];
NSLog(@"todayDate: %@", todayDate);
NSLog(@"afterSevenDays: %@", afterSevenDays);
于 2013-03-12T05:56:38.717 回答
1
  NSCalendar *calendar=[NSCalendar currentCalendar];    
  NSDateComponents *components = [[NSDateComponents alloc]init];
components.day = 7;
NSDate *fireDate =[calendar dateByAddingComponents:components toDate:[NSDate date] options:0];
于 2013-03-12T05:57:21.087 回答