4

I need to get time difference from two time stamp fields, i.e., My input : "requestTime" => "2016-12-27 18:35:13:833", "responseTime" => "2016-12-27 18:35:13:834",

I Need to get time diff as 1 milliseconds as a result.

I used this code :

 event['time_difference']= (Time.parse(event['responseTime']).to_i) - (Time.parse(event['requestTime']).to_i)

I got result as 0 seconds for above input.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
Jeeva N
  • 431
  • 1
  • 5
  • 17
  • How it will be duplicate, its about the time format time to millisecond before applying the difference. There, it was said like apply the difference and convert the seconds to millisecond. i thing both have some difference – Jeeva N Dec 28 '16 at 09:52

1 Answers1

3

Use to_f

millisec = 1000 * (response_time.to_f - request_time.to_f)

The timestamps must have this format though,

"2016-12-27 18:35:13.833"

Notice the . separator between seconds and milliseconds!

akuhn
  • 27,477
  • 2
  • 76
  • 91