Ruby – Get current system time in milliseconds

ruby

In Ruby, what is the right way to get the current system time since epoch(1970) in milliseconds?

I tried Time.now.to_i , it seems not the result I want. I need the result shows milliseconds and with long type, not float or double.

Best Answer

(Time.now.to_f * 1000).to_i

Time.now.to_f shows you the time including decimal numbers. To get number of miliseconds just multiply the time by 1000.