Search code examples
timeswift2

Swift 2 get minutes and seconds from double


I am currently storing time in seconds (for example 70.149 seconds). How would I easily get the minutes and seconds? What I mean is 70.149 seconds is 1min and 10sec. How would I be able to do this easily in swift?

Here is an example of what I want to do.

let time:Double = 70.149 //This can be any value
let mins:Int = time.minutes //In this case it would be 1
let secs:Int = time.seconds //And this would be 10

How would I do this using Swift 2 OS X (not iOS)


Solution

  • Something like this:

    let temp:Int = Int(time + 0.5) // Rounding
    let mins:Int = temp / 60
    let secs:Int = temp % 60