Swift - Integer conversion to Hours/Minutes/Seconds

后端 未结 23 1135
無奈伤痛
無奈伤痛 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 01:58

    Here is what I use for my Music Player in Swift 4+. I am converting seconds Int to readable String format

    extension Int {
        var toAudioString: String {
            let h = self / 3600
            let m = (self % 3600) / 60
            let s = (self % 3600) % 60
            return h > 0 ? String(format: "%1d:%02d:%02d", h, m, s) : String(format: "%1d:%02d", m, s)
        }
    }
    

    Use like this:

    print(7903.toAudioString)
    

    Output: 2:11:43

    0 讨论(0)
  • 2020-11-28 01:59

    Here is another simple implementation in Swift3.

    func seconds2Timestamp(intSeconds:Int)->String {
       let mins:Int = intSeconds/60
       let hours:Int = mins/60
       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)
  • 2020-11-28 01:59

    Answer of @r3dm4n was great. However, I needed also hour in it. Just in case someone else needed too here it is:

    func formatSecondsToString(_ seconds: TimeInterval) -> String {
        if seconds.isNaN {
            return "00:00:00"
        }
        let sec = Int(seconds.truncatingRemainder(dividingBy: 60))
        let min = Int(seconds.truncatingRemainder(dividingBy: 3600) / 60)
        let hour = Int(seconds / 3600)
        return String(format: "%02d:%02d:%02d", hour, min, sec)
    }
    
    0 讨论(0)
  • 2020-11-28 02:03

    In macOS 10.10+ / iOS 8.0+ (NS)DateComponentsFormatter has been introduced to create a readable string.

    It considers the user's locale und language.

    let interval = 27005
    
    let formatter = DateComponentsFormatter()
    formatter.allowedUnits = [.hour, .minute, .second]
    formatter.unitsStyle = .full
    
    let formattedString = formatter.string(from: TimeInterval(interval))!
    print(formattedString)
    

    The available unit styles are positional, abbreviated, short, full, spellOut and brief.

    For more information please read the documenation.

    0 讨论(0)
  • 2020-11-28 02:04

    Swift 4

    func formatSecondsToString(_ seconds: TimeInterval) -> String {
        if seconds.isNaN {
            return "00:00"
        }
        let Min = Int(seconds / 60)
        let Sec = Int(seconds.truncatingRemainder(dividingBy: 60))
        return String(format: "%02d:%02d", Min, Sec)
    }
    
    0 讨论(0)
  • 2020-11-28 02:05

    I have built a mashup of existing answers to simplify everything and reduce the amount of code needed for Swift 3.

    func hmsFrom(seconds: Int, completion: @escaping (_ hours: Int, _ minutes: Int, _ seconds: Int)->()) {
    
            completion(seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
    
    }
    
    func getStringFrom(seconds: Int) -> String {
    
        return seconds < 10 ? "0\(seconds)" : "\(seconds)"
    }
    

    Usage:

    var seconds: Int = 100
    
    hmsFrom(seconds: seconds) { hours, minutes, seconds in
    
        let hours = getStringFrom(seconds: hours)
        let minutes = getStringFrom(seconds: minutes)
        let seconds = getStringFrom(seconds: seconds)
    
        print("\(hours):\(minutes):\(seconds)")                
    }
    

    Prints:

    00:01:40

    0 讨论(0)
提交回复
热议问题