Juma Orido
Juma Orido

Reputation: 491

Format timer label to hours:minutes:seconds in Swift

I have an NSTimer which counts DOWN from 2 hours until 0.

Here are some of my code:

var timer = NSTimer()
let timeInterval:NSTimeInterval = 0.5
let timerEnd:NSTimeInterval = 0.0
var timeCount:NSTimeInterval = 7200.0 // seconds or 2 hours

// TimeString Function

func timeString(time:NSTimeInterval) -> String {
    let minutes = Int(time) / 60
    let seconds = time - Double(minutes) * 60
    let secondsFraction = seconds - Double(Int(seconds))
    return String(format:"%02i:%02i.%01i",minutes,Int(seconds),Int(secondsFraction * 10.0))
}

The Timer Label is:

TimerLabel.text = "Time: \(timeString(timeCount))"

HOWEVER, my timer label shows as:

Time: 200:59.0

How do I format my timer label to look like this:

Time: 01:59:59 // (which is hours:minutes:seconds)?

[Please note that I have no problems with my countdown timer, I only need to know how to CHANGE THE TIME FORMAT using the TimeString function.]

EDIT: Someone mentioned that my question is a possible duplicate of this one: Swift - iOS - Dates and times in different format. HOWEVER, I am asking on how do I change the time format using the TimeString function that I gave above. I am not asking for another WAY on how to do it.

For instance:

let minutes = Int(time) / 60

gives me "200" minutes. etc.

Upvotes: 42

Views: 43967

Answers (5)

Rohit Nishad
Rohit Nishad

Reputation: 448

Swift5

var totalSecond = Int()
var timer:Timer?

call startTimer() based on requirement-

func startTimer(){
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countdown), userInfo: nil, repeats: true)
}

@objc func countdown() {
    var hours: Int
    var minutes: Int
    var seconds: Int

    if totalSecond == 0 {
        timer?.invalidate()
    }
    totalSecond = totalSecond - 1
    hours = totalSecond / 3600
    minutes = (totalSecond % 3600) / 60
    seconds = (totalSecond % 3600) % 60
    timeLabel.text = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}

Done

Upvotes: 7

oscar castellon
oscar castellon

Reputation: 3138

The best way to implement a Timer in Swift (swift 4 works fine). Declare the variable secs: Int and assign the value, in seconds, of the timer. Then with the Timer () function, discount one second at a time and pass it to this function.

var secs = 0
var timer = Timer()

func startTimer(segs: Int) {
        seg = segs
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerDiscount), userInfo: nil, repeats: true)
    }

func timerDiscount() {
            let hours = secs / 3600
            let mins = secs / 60 % 60
            let secs = secs % 60
            let restTime = ((hours<10) ? "0" : "") + String(hours) + ":" + ((mins<10) ? "0" : "") + String(mins) + ":" + ((secs<10) ? "0" : "") + String(secs)
}

Upvotes: 1

Prashanth Thota
Prashanth Thota

Reputation: 166

Declare the variables hours ,minutes and seconds and copy paste the below code it works fine.

      if counter > 0 {

        let hours = counter / 3600

        let minutes = counter / 60

        let seconds = counter % 60

        counter = counter - 1

        timerLbl.text = "\(hours):\(minutes):\(seconds)"

    }

Upvotes: 0

Mark Suman
Mark Suman

Reputation: 10460

@rmaddy's solution is accurate and answers the question. However, neither the question nor the solution take into account international users. I suggest using DateComponentsFormatter and let the framework handle the calculations and formatting. Doing so makes your code less error prone and more future proof.

I came across this blog post that provides a concise solution: http://crunchybagel.com/formatting-a-duration-with-nsdatecomponentsformatter/

Pulled from that post, this is the code snippet that would replace the code you're currently using to make your calculations. Updated for Swift 3:

let duration: TimeInterval = 7200.0

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional // Use the appropriate positioning for the current locale
formatter.allowedUnits = [ .hour, .minute, .second ] // Units to display in the formatted string
formatter.zeroFormattingBehavior = [ .pad ] // Pad with zeroes where appropriate for the locale

let formattedDuration = formatter.string(from: duration) 

Upvotes: 94

rmaddy
rmaddy

Reputation: 318774

Your calculations are all wrong.

let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format:"%02i:%02i:%02i", hours, minutes, seconds)

Upvotes: 114

Related Questions