这就是我设定 future 日期的方式:
var comp = NSDateComponents()
comp.setValue(1, forComponent: NSCalendarUnit.CalendarUnitMonth);
let date: NSDate = NSDate()
var expirationDate = NSCalendar.currentCalendar().dateByAddingComponents(comp, toDate: date, options: NSCalendarOptions(0))
现在,如果我有 dateA 并想创建一个比 dateA 早 1 秒的新日期 (dateB),该怎么办?
请您参考如下方法:
You can achieve this by changing your second line from
1 to -1and NSCalendarUnitMonth to Second
var comp = DateComponents()
comp.setValue(-1, for: .second)
let date = Date()
let expirationDate = NSCalendar.current.date(byAdding: comp, to: date)
希望对你有所帮助。
