Nrc
Nrc

Reputation: 9787

MInutes and seconds with 2 digits

I have an input in seconds I store it in a var:

var totalTime = 60

I want to show it with 2 digits for minutes and 2 digits for seconds:
01:00

What I have tried:

let minutes = String(totalTime / 60)  
let seconds = String(totalTime % 60)
label.text = minutes + ":" + seconds  

This gives me: 1:0 But I want 01:00

I tried and It does not work:

label.text = String(format: "%02d:%02d", minutes, seconds)

Upvotes: 0

Views: 638

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236260

The problem with your second approach is that it expects an integer and you are passing a string.

let minutes = totalTime / 60 
let seconds = totalTime % 60

label.text = String(format: "%02d:%02d", minutes, seconds)

Upvotes: 3

Related Questions