Search code examples
iosswiftnstimeinterval

How to customize my timer display to show only minutes and seconds?


My Timer is displaying Minutes and Hours, but once it gets to 60 minutes it restarts from 0 Minute.

  • Should I get rid of the modulo ( % 60 ) for minutes.

  • I would like my timer to display for ex: 80:45 ( basically not stopping at 60 min once it reaches 1 hour)

    var min = 0
    var sec = 0
    
    func stringFromTimeInterval(interval: NSTimeInterval) -> String {
         let interval = Int(interval)
         let seconds = interval % 60
         let minutes = (interval / 60) % 60
         //let hours = (interval / 3600)// I don't need the hours 
         return String(format: "%02d:%02d",minutes, seconds)
     }
    

Solution

  • % 60 means that it will spit out a minutes value that is the remainder when divided by 60(minutes). This is most probably because for time in the form hh:mm, you want it to go from 5:59 to 6:00, not 5:60. So changing the following line will give you what you seek.

    let minutes = (interval / 60) % 60 -> let minutes = interval / 60