How to add minutes to current time in swift

前端 未结 10 1375
天涯浪人
天涯浪人 2020-11-27 03:10

I am new to Swift and am trying a scheduler. I have the start time selected and I need to add 5 minutes (or multiples of it) to the start time and display it in an UILabel?<

10条回答
  •  孤街浪徒
    2020-11-27 03:33

    You can do date arithmetic by using NSDateComponents. For example:

    import Foundation
    
    let comps = NSDateComponents()
    
    comps.minute = 5
    
    let cal = NSCalendar.currentCalendar()
    
    let r = cal.dateByAddingComponents(comps, toDate: NSDate(), options: nil)
    

    It is what you see when you try it in playground

    enter image description here

提交回复
热议问题