or *_*ran 74 iphone objective-c nsdate ios
如何将月添加到NSDate对象?
NSDate *someDate = [NSDate Date] + 30Days.....;
Run Code Online (Sandbox Code Playgroud)
The*_*Eye 137
您需要使用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
Run Code Online (Sandbox Code Playgroud)
Kev*_*vin 115
使用iOS 8和OS X 10.9,您可以NSCalendarUnits使用NSCalendar以下命令添加:
Objective-C的
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *someDate = [cal dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:[NSDate date] options:0];
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
let date = Calendar.current.date(byAdding: .month, value: 1, to: Date())
Run Code Online (Sandbox Code Playgroud)
斯威夫特2
let cal = NSCalendar.currentCalendar()
let date = cal.dateByAddingUnit(.Month, value: 1, toDate: NSDate(), options: [])
Run Code Online (Sandbox Code Playgroud)
Soo*_*ark 17
对于swift 3.0
extension Date {
func addMonth(n: Int) -> Date {
let cal = NSCalendar.current
return cal.date(byAdding: .month, value: n, to: self)!
}
func addDay(n: Int) -> Date {
let cal = NSCalendar.current
return cal.date(byAdding: .day, value: n, to: self)!
}
func addSec(n: Int) -> Date {
let cal = NSCalendar.current
return cal.date(byAdding: .second, value: n, to: self)!
}
}
Run Code Online (Sandbox Code Playgroud)
Aar*_*ger 12
例如,要3在Swift中向当前日期添加月份:
let date = NSCalendar.currentCalendar().dateByAddingUnit(.MonthCalendarUnit, value: 3, toDate: NSDate(), options: nil)!
Run Code Online (Sandbox Code Playgroud)
let date = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 3, toDate: NSDate(), options: [])
Run Code Online (Sandbox Code Playgroud)
OptionSetType结构NSCalendarUnit让您更简单地指定.MonthOptionSetType的options:参数(如参数,所采取的NSCalendarOptions)不能nil,因此传入一个空的set([])来表示"无选项".| 归档时间: |
|
| 查看次数: |
52946 次 |
| 最近记录: |