How to add one month to an NSDate?

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

How To Add Month To NSDate Object?

NSDate *someDate = [NSDate Date] + 30Days.....;
9条回答
  •  星月不相逢
    2020-12-23 11:34

    For 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)!
        }
    }
    

提交回复
热议问题