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

后端 未结 7 494
闹比i
闹比i 2020-12-04 11:35

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         


        
7条回答
  •  没有蜡笔的小新
    2020-12-04 12:22

    Javascript's gettime() returns the number of milliseconds since epoch.

    Ruby's Time.now.to_i will give you the number of seconds since epoch. If you change that to Time.now.to_f, you still get seconds but with a fractional component. Just multiply that by 1,000 and you have milliseconds. Then use #to_i to convert it to an integer. And you end up with:

    (Time.now.to_f * 1000).to_i
    

提交回复
热议问题