Ruby – How to get the current time as 13-digit integer in Ruby

ruby

I have this jQuery function that returns the current time as the number of milliseconds since the epoch (Jan 1, 1970):

time = new Date().getTime();

Is there a way to do the same in Ruby?

Right now, I am using Ruby's Time.now.to_i which works great but returns a 10-digit integer (number of seconds)

How can I get it to display the number of milliseconds, as in jQuery?

Best Answer

require 'date'

p DateTime.now.strftime('%s') # "1384526946" (seconds)
p DateTime.now.strftime('%Q') # "1384526946523" (milliseconds)