0

*I have a timer in my main. storyboard I have a link to a title where I need it to show the minutes and seconds like 25:52 how can I show the minute and seconds?

here is my code for the timer.*

    timer3 = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(UIOutlitsViewController.action), userInfo: nil, repeats: true)
}
@objc func action() {
    time += 1
    timeView1.text = String(time)
    if time == 5.0 {
        btnPressed1.titleLabel?.textColor = UIColor.red
        
         btnPressed1.isUserInteractionEnabled = true
            
        
        
        let url = Bundle.main.url(forResource: "mixkit-alarm-clock-beep-988", withExtension: "wav")
        player = try! AVAudioPlayer(contentsOf: url!)
        player.play()
        timer3.invalidate()
      
    }
}
  • 1
    [Formatting a Duration with NSDateComponentsFormatter](https://crunchybagel.com/formatting-a-duration-with-nsdatecomponentsformatter/); [Formatting a Time Interval in Swift](https://cocoacasts.com/cocoa-fundamentals-formatting-a-time-interval-in-swift); [How to format time intervals for user display (social network like) in swift?](https://stackoverflow.com/questions/35999022/how-to-format-time-intervals-for-user-display-social-network-like-in-swift/35999455) – MadProgrammer Jan 19 '22 at 21:42

1 Answers1

1

Firstly You have to change time in hour and minute.

      minutes = (totalSecond % 3600) / 60
      seconds = (totalSecond % 3600) % 60

After that you have to change in Hour:Minute format.

timeView1.text = String(format: "%02d:%02d",minutes,seconds)
        
Saurabh Pathak
  • 879
  • 7
  • 10