如何在 Ruby 中获取当前时间为 13 位整数? [英] How to get the current time as 13-digit integer in Ruby?
本文介绍了如何在 Ruby 中获取当前时间为 13 位整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个 jQuery
函数,它返回当前时间作为自纪元(1970 年 1 月 1 日)以来的毫秒数:
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();
有没有办法在 Ruby 中做同样的事情?
Is there a way to do the same in Ruby?
现在,我正在使用 Ruby 的 Time.now.to_i
,它运行良好,但返回一个 10 位 整数(秒数)
Right now, I am using Ruby's Time.now.to_i
which works great but returns a 10-digit integer (number of seconds)
我怎样才能让它显示毫秒数,就像在 jQuery
中一样?
How can I get it to display the number of milliseconds, as in jQuery
?
推荐答案
require 'date'
p DateTime.now.strftime('%s') # "1384526946" (seconds)
p DateTime.now.strftime('%Q') # "1384526946523" (milliseconds)
这篇关于如何在 Ruby 中获取当前时间为 13 位整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文