How to add minutes to current time in swift

前端 未结 10 1358
天涯浪人
天涯浪人 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

    Save this little extension:

    extension Int {
    
     var seconds: Int {
        return self
     }
    
     var minutes: Int {
        return self.seconds * 60
     }
    
     var hours: Int {
        return self.minutes * 60
     }
    
     var days: Int {
        return self.hours * 24
     }
    
     var weeks: Int {
        return self.days * 7
     }
    
     var months: Int {
        return self.weeks * 4
     }
    
     var years: Int {
        return self.months * 12
     }
    }
    

    Then use it intuitively like:

    let threeDaysLater = TimeInterval(3.days)
    date.addingTimeInterval(threeDaysLater)
    

提交回复
热议问题