【问题标题】:How to sum array of time Duration Which is in string format如何对字符串格式的时间数组求和
【发布时间】:2018-06-04 11:30:14
【问题描述】:

我有一个格式如下的字符串数组,

let sample_array = ["05:30","06:20","04:20","09:40"]

当我们将所有string转换为DATE格式后,我们如何从这个数组中找到总时间。

【问题讨论】:

  • 你所说的“总时间”是什么意思?
  • 数组就像 ["04:30" , "04:00"] ,所以总小时 08:30。像那样@AhmadF
  • 你能解释一下如何添加持续时间@Moritz
  • 你尝试了什么???
  • 我只是尝试将字符串转换为日期格式并尝试@YagneshDobariya

标签: ios arrays swift nsdate nsdateformatter


【解决方案1】:

我认为您可以跳过将字符串转换为日期以获得所需的输出:

let sample_array = ["05:30","06:20","04:20","09:40"]
var hours:Int = 0
var minutes:Int = 0
for timeString in sample_array {
    let components = timeString.components(separatedBy: ":")
    let hourComp = Int(components.first ?? "0") ?? 0
    let minComp = Int(components.last ?? "0") ?? 0
    hours += hourComp
    minutes += minComp
}
hours += minutes/60
minutes = minutes%60
let hoursString = hours > 9 ? hours.description : "0\(hours)"
let minsString = minutes > 9 ? minutes.description : "0\(minutes)"
let totalTime = hoursString+":"+minsString

【讨论】:

  • 成功了。对不起,我不能赞成你的回答。至少需要 15 名声望。 =D,谢谢@Puneet
  • @Thug__ 如果它适合你,你应该接受它作为有效答案
【解决方案2】:

对于您的情况,我建议不要将其视为日期来处理它。您可以通过实现以下功能来获得所需的结果:

func getTotalTime(_ array: [String]) -> String {
    // getting the summation of minutes and seconds
    var minutesSummation = 0
    var secondsSummation = 0

    array.forEach { string in
        minutesSummation += Int(string.components(separatedBy: ":").first ?? "0")!
        secondsSummation += Int(string.components(separatedBy: ":").last ?? "0")!
    }

    // converting seconds to minutes
    let minutesInSeconds = secondsToMinutes(seconds: secondsSummation).0
    let restOfSeconds = secondsToMinutes(seconds: secondsSummation).1

    return "\(minutesSummation + minutesInSeconds):\(restOfSeconds)"
}

// https://stackoverflow.com/questions/26794703/swift-integer-conversion-to-hours-minutes-seconds
func secondsToMinutes (seconds : Int) -> (Int, Int) {
    return ((seconds % 3600) / 60, (seconds % 3600) % 60)
}

因此:

let array = ["20:40" , "20:40"]
let result = getTotalTime(array)
print(result) // 41:20

【讨论】:

    【解决方案3】:

    从问题和 cmets 看来,您正在尝试从给定数组中计算总时间(以小时和分钟为单位)。

    let sample_array = ["05:30","06:20","04:20","09:40"]
    
    func getTime(arr: [String]) -> Int {
    
      var total = 0
      for obj in arr {
    
          let comp = obj.split(separator: ":")
    
          var hours = 0
          var minutes = 0
          if let hr = comp.first, let h = Int(String(hr)) {
            hours = h * 60
          }
    
          if let mn = comp.last, let min = Int(String(mn)) {
            minutes = min
          }
    
          total += hours
          total += minutes
    
        }
        return total
    
    }
    
    let totalTime = getTime(arr: sample_array)
    print(totalTime)
    
    let hours = totalTime/60
    let minutes = totalTime%60
    print("\(hours) hours and \(minutes) minutes")
    

    您还可以进一步计算日、月和年。

    我希望这是你想要的。

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 2010-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多