iOS已知时间加N天(减N天)后的日期
- (void)addDay {
NSInteger distance = 3; // 加的天数
NSDate *nowDate = [NSDate date];
NSDate *otherDate;
NSTimeInterval oneDay = 24 * 60 * 60 * 1; //1天的长度
// 加N天
otherDate = [nowDate initWithTimeIntervalSinceNow: +oneDay* distance ];
// 减去
//theDate = [nowDate initWithTimeIntervalSinceNow: -oneDay*dis ];
// 方法2
NSDate *newDate = [nowDate dateByAddingTimeInterval:24 * 60 * 60 * distance];
NSLog(@"\n现在的时间%@\nN天后: %@\n方法2N天后newDate=%@", nowDate, otherDate, newDate);
}
现在的时间 2017-07-19 11:55:04 +0000
N天后: 2017-07-22 11:55:04 +0000
方法2N天后newDate=2017-07-22 11:55:04 +0000