Swift - Integer conversion to Hours/Minutes/Seconds

后端 未结 23 1244
無奈伤痛
無奈伤痛 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:12

    SWIFT 3.0 solution based roughly on the one above using extensions.

    extension CMTime {
      var durationText:String {
        let totalSeconds = CMTimeGetSeconds(self)
        let hours:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 86400) / 3600)
        let minutes:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 3600) / 60)
        let seconds:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 60))
    
        if hours > 0 {
            return String(format: "%i:%02i:%02i", hours, minutes, seconds)
        } else {
            return String(format: "%02i:%02i", minutes, seconds)
        }
    
      }
    }
    

    Use it with AVPlayer calling it like this?

     let dTotalSeconds = self.player.currentTime()
     playingCurrentTime = dTotalSeconds.durationText
    

提交回复
热议问题