-2

I am working in networking swizzling of URLSession and getting every request details. However when I am trying to calculate value of request date in milliseconds, sometimes calculation is showing much higher value. In swizzling of startLoading we are taking current date and inside stopLoading we are calculating milliseconds. Below is my logic

let startDate: Date?
override public func startLoading() {
    startDate = Date()
}

override public func stopLoading() {
   print(fabs(startDate.timeIntervalSinceNow) * 1000)


 }
Pratik Sodha
  • 3,679
  • 2
  • 19
  • 38
Kashif Jilani
  • 1,207
  • 3
  • 22
  • 49

3 Answers3

12

In your stopLoading function you should call:

print(Date().timeIntervalSince(startDate) * 1000)

This way you will see how much time (in ms) has passed since startDate.

Razvan S.
  • 753
  • 7
  • 21
2

Another way to find difference between two dates using Calender

let diff = Calendar.current.dateComponents([.second], from: Date1, to: Date2).second
let millisecond = diff * 1000
print(millisecond)
Abhi Yaduwanshi
  • 380
  • 3
  • 12
0

Date() function as it says in the Documentation is returning UTC time. Have you checked if the startDate variable is also UTC time? You should probably calculate also the difference bettween the users time zone and the UTC time.

Vasilis D.
  • 1,416
  • 13
  • 21