Swift - Integer conversion to Hours/Minutes/Seconds

后端 未结 23 1247
無奈伤痛
無奈伤痛 2020-11-28 01:47

I have a (somewhat?) basic question regarding time conversions in Swift.

I have an integer that I would like converted into Hours / Minutes / Second

23条回答
  •  甜味超标
    2020-11-28 02:05

    neek's answer isn't correct.

    here's the correct version

    func seconds2Timestamp(intSeconds:Int)->String {
       let mins:Int = (intSeconds/60)%60
       let hours:Int = intSeconds/3600
       let secs:Int = intSeconds%60
    
       let strTimestamp:String = ((hours<10) ? "0" : "") + String(hours) + ":" + ((mins<10) ? "0" : "") + String(mins) + ":" + ((secs<10) ? "0" : "") + String(secs)
       return strTimestamp
    }
    

提交回复
热议问题