"2016-12-27 18:35:13:833", "responseTime" => "2016-12-27..." /> "2016-12-27 18:35:13:833", "responseTime" => "2016-12-27..." /> "2016-12-27 18:35:13:833", "responseTime" => "2016-12-27..."/>

How to find time difference in milliseconds from two datetime stamp field using ruby

3.3k Views Asked by At

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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!