i Have an array of strings with the following format,
let sample_array = ["05:30","06:20","04:20","09:40"]
When after we converted all string
to DATE
format ,How can we find the total time from this array.
I think you can skip converting string to date for achieving your desired output:
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