How to add one month to an NSDate?

前端 未结 9 2140
自闭症患者
自闭症患者 2020-12-23 11:01

How To Add Month To NSDate Object?

NSDate *someDate = [NSDate Date] + 30Days.....;
9条回答
  •  执笔经年
    2020-12-23 11:37

    For example, to add 3 months to the current date in Swift:

    let date = NSCalendar.currentCalendar().dateByAddingUnit(.MonthCalendarUnit, value: 3, toDate: NSDate(), options: nil)!
    

    In Swift 2.0:

    let date = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 3, toDate: NSDate(), options: [])
    
    • The new OptionSetType structure of NSCalendarUnit lets you more simply specify .Month
    • Parameters that take OptionSetType (like the options: parameter, which takes NSCalendarOptions) can't be nil, so pass in an empty set ([]) to represent "no options".

提交回复
热议问题