美文网首页
时分秒相加

时分秒相加

作者: UILabelkell | 来源:发表于2022-05-08 21:19 被阅读0次

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

   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)
        }
        ```


请各位路过的大神指教

相关文章

网友评论

      本文标题:时分秒相加

      本文链接:https://www.haomeiwen.com/subject/qnxcurtx.html