How to reduce few minutes for current date in Swift 2.0 [duplicate]

白昼怎懂夜的黑 提交于 2019-12-13 08:50:32

问题


How can I reduce 5 or 10 minutes from current time and then store it as date format in Swift 2.0 ?

let now = NSDate()
var reducedTime = ???? \\ Here I want 10 minutes reduced from current time.

If current time is 02:20:00, then reducedTime should be 02:10:00 How can I do this in a simplest way ???


回答1:


You can use calendar method dateByAddingUnit and subtract 10 minutes fro the date.

let now = NSDate()

let reducedTime = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)?.dateByAddingUnit(.Minute, value: -10, toDate: now, options: NSCalendarOptions())


来源:https://stackoverflow.com/questions/33025860/how-to-reduce-few-minutes-for-current-date-in-swift-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!