ios - 如何为 NSDate 添加一个月?

如何向 NSDate 对象添加月份?

NSDate *someDate = [NSDate Date] + 30Days.....;

最佳答案

你需要使用NSDateComponents:

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:originalDate options:0];
[dateComponents release]; // If ARC is not used, release the date components

https://stackoverflow.com/questions/8119880/

相关文章:

ios - "interfaceOrientation"在 iOS 8 中已弃用,如何更改此方法 O

ios - 找不到符号 : kUTTypeImage

ios - NSPredicate 用于测试 NULL 和空白字符串

objective-c - 有什么方法可以加粗 NSString 的一部分?

ios - 在主线程上调用方法?

iphone - 如何使用 NSNotificationcenter 的 object 属性

ios - 如何调用 iPhone map 以获取当前位置作为起始地址的方向

objective-c - 为什么 nil/NULL block 在运行时会导致总线错误?

objective-c - 删除 NSString 的最后一个字符

ios - 如何检查是否为 64 位构建了静态库?