Swift - Integer conversion to Hours/Minutes/Seconds

后端 未结 23 1138
無奈伤痛
無奈伤痛 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
    }
    
    0 讨论(0)
  • Swift 5:

    extension Int {
    
        func secondsToTime() -> String {
    
            let (h,m,s) = (self / 3600, (self % 3600) / 60, (self % 3600) % 60)
    
            let h_string = h < 10 ? "0\(h)" : "\(h)"
            let m_string =  m < 10 ? "0\(m)" : "\(m)"
            let s_string =  s < 10 ? "0\(s)" : "\(s)"
    
            return "\(h_string):\(m_string):\(s_string)"
        }
    }
    

    Usage:

    let seconds : Int = 119
    print(seconds.secondsToTime()) // Result = "00:01:59"
    
    0 讨论(0)
  • 2020-11-28 02:06

    NSTimeInterval is Double do do it with extension. Example:

    extension Double {
    
        var formattedTime: String {
    
            var formattedTime = "0:00"
    
            if self > 0 {
    
                let hours = Int(self / 3600)
                let minutes = Int(truncatingRemainder(dividingBy: 3600) / 60)
    
                formattedTime = String(hours) + ":" + (minutes < 10 ? "0" + String(minutes) : String(minutes))
            }
    
            return formattedTime
        }
    }
    
    0 讨论(0)
  • 2020-11-28 02:10

    According to GoZoner answer I have wrote an Extension to get the time formatted according to the hours, minute, and seconds:

    extension Double {
    
        func secondsToHoursMinutesSeconds () -> (Int?, Int?, Int?) {
            let hrs = self / 3600
            let mins = (self.truncatingRemainder(dividingBy: 3600)) / 60
            let seconds = (self.truncatingRemainder(dividingBy:3600)).truncatingRemainder(dividingBy:60)
            return (Int(hrs) > 0 ? Int(hrs) : nil , Int(mins) > 0 ? Int(mins) : nil, Int(seconds) > 0 ? Int(seconds) : nil)
        }
    
        func printSecondsToHoursMinutesSeconds () -> String {
    
            let time = self.secondsToHoursMinutesSeconds()
    
            switch time {
            case (nil, let x? , let y?):
                return "\(x) min \(y) sec"
            case (nil, let x?, nil):
                return "\(x) min"
            case (let x?, nil, nil):
                return "\(x) hr"
            case (nil, nil, let x?):
                return "\(x) sec"
            case (let x?, nil, let z?):
                return "\(x) hr \(z) sec"
            case (let x?, let y?, nil):
                return "\(x) hr \(y) min"
            case (let x?, let y?, let z?):
                return "\(x) hr \(y) min \(z) sec"
            default:
                return "n/a"
            }
        }
    }
    
    let tmp = 3213123.printSecondsToHoursMinutesSeconds() // "892 hr 32 min 3 sec"
    
    0 讨论(0)
  • 2020-11-28 02:10

    Latest Code: XCode 10.4 Swift 5

    extension Int {
        func timeDisplay() -> String {
            return "\(self / 3600):\((self % 3600) / 60):\((self % 3600) % 60)"
        }
    }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题