时分秒相加

最近遇到了一个需求,就是时分秒 时间相加,然后自己写了一个方法

   func timeTotimeSring(time1: String, time2: String) -> String{
        let strAry1  = time1.split(separator: ":")
        let strAry2  = time2.split(separator: ":")
        print(strAry1)
        print(strAry2)
        let hour    = Int(strAry1[0])! + Int(strAry2[0])!
        let minutes = Int(strAry1[1])! + Int(strAry2[1])!
        let seconds = Int(strAry1[2])! + Int(strAry2[2])!
        
        let sec = Int(minutes / 60)
        let sec1 = Int(Double(minutes).truncatingRemainder(dividingBy: 60))
        let hour1 = hour + sec   /// 分钟 到60 加1
        
        let extry = Int(seconds / 60)
        let extry1 = Int(Double(seconds).truncatingRemainder(dividingBy: 60))
        let minutes2 = sec1 + extry。 /// 秒 到60 加1
        let seconds1 = extry1
        
        return String(hour1) + ":" + String(minutes2) + ":" + String(seconds1)

    }

用法:

    var str = "00:00:00"
        let ary:Array = ["06:15:34","12:06:03","03:54:59"]
        for index in 0 ... ary.count-1 {
            str = timeTotimeSring(time1: str, time2: ary[index])
            print("==",str)
        }
        ```


请各位路过的大神指教

你可能感兴趣的:(时分秒相加)