20

I have 2 variables where I get 2 times from datePicker and I need to save on a variable the difference between them.

    let timeFormatter = DateFormatter()
    timeFormatter.dateFormat = "HHmm"

    time2 = timeFormatter.date(from: timeFormatter.string(from: datePicker.date))!

I have tried to get the timeIntervalSince1970 from both of them and them substract them and get the difference on milliseconds which I will turn back to hours and minutes, but I get a very big number which doesn't corresponds to the actual time.

let dateTest = time2.timeIntervalSince1970 - time1.timeIntervalSince1970

Then I have tried using time2.timeIntervalSince(date: time1), but again the result milliseconds are much much more than the actual time.

How I can get the correct time difference between 2 times and have the result as hours and minutes in format "0823" for 8 hours and 23 minutes?

ToroLoco
  • 461
  • 1
  • 5
  • 20

4 Answers4

27

The recommended way to do any date math is Calendar and DateComponents

let difference = Calendar.current.dateComponents([.hour, .minute], from: time1, to: time2)
let formattedString = String(format: "%02ld%02ld", difference.hour!, difference.minute!)
print(formattedString)

The format %02ld adds the padding zero.

If you need a standard format with a colon between hours and minutes DateComponentsFormatter() could be a more convenient way

let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute]
print(formatter.string(from: time1, to: time2)!)
vadian
  • 274,689
  • 30
  • 353
  • 361
  • According to your code `time1` and `time2` are supposed to be `Date` instances. A date formatter is not needed. – vadian Dec 25 '17 at 16:44
  • When I does this with code I get following as printout which is not correct: "fallAsleep: 2017-12-25 21:51:49 +0000, WakeUp: 2017-12-25 06:49:55 +0000, Difference: 1501" – ToroLoco Dec 25 '17 at 16:53
  • But i just noticed that the mistake is because it calculates the same day. – ToroLoco Dec 25 '17 at 16:54
  • If you are using dates without the day component add one day to `wake` if `wake` < `sleep` – vadian Dec 25 '17 at 16:59
  • ok, I have added `if timePicker.date <= fallAsleep { timePicker.date = timePicker.date.addingTimeInterval(+86400) }` and I am adding 24 hours on the second date to handle the day difference. – ToroLoco Dec 26 '17 at 11:26
18

TimeInterval measures seconds, not milliseconds:

let date1 = Date()
let date2 = Date(timeIntervalSinceNow: 12600) // 3:30

let diff = Int(date2.timeIntervalSince1970 - date1.timeIntervalSince1970)

let hours = diff / 3600
let minutes = (diff - hours * 3600) / 60
Gereon
  • 17,258
  • 4
  • 42
  • 73
9

To get duration in seconds between two time intervals, this can be used -

let time1 = Date(timeIntervalSince1970: startTime)
let time2 = Date(timeIntervalSince1970: endTime)
let difference = Calendar.current.dateComponents([.second], from: time1, to: time2)
let duration = difference.second
Namit Gupta
  • 796
  • 9
  • 20
8

Now you can do it in swift 5 this way,

func getDateDiff(start: Date, end: Date) -> Int  {
    let calendar = Calendar.current
    let dateComponents = calendar.dateComponents([Calendar.Component.second], from: start, to: end)

    let seconds = dateComponents.second
    return Int(seconds!)
}
zengr
  • 38,346
  • 37
  • 130
  • 192
cary
  • 148
  • 2
  • 7