operation with hours using swift, sum and difference

On my project I need to work with hours and min. I can't find to much info online.

here my issue:

let time1 = "22:00"
let time2 = "20:00"

Question 1:

how do I subtract the time1 - time2 ? I'm expecting result = 02:00 h

i start writing some code, converting this time to date..

let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
var time1d = formatter.date(from: time1)!
let time2d = formatter.date(from: time2)!

but now how do I subtract this two hours?

Question 2:

let time1 = "22:00"
let time2 = "20:00"

How do i sum time1 + time2 ? for example should give me result 42:00 hours

thanks for the help



Read more here: https://stackoverflow.com/questions/68463104/operation-with-hours-using-swift-sum-and-difference

Content Attribution

This content was originally published by Damiano Miazzi at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: