将Rubydate转换为整数
如何将一个Rubydate转换为一个整数?
t = Time.now # => 2010-12-20 11:20:31 -0700 # Seconds since epoch t.to_i #=> 1292869231 require 'date' d = Date.today #=> #<Date: 2010-12-20 (4911101/2,0,2299161)> epoch = Date.new(1970,1,1) #=> #<Date: 1970-01-01 (4881175/2,0,2299161)> d - epoch #=> (14963/1) # Days since epoch (d - epoch).to_i #=> 14963 # Seconds since epoch d.to_time.to_i #=> 1292828400
Time.now.to_i
自纪元格式以来的秒数
当你有一个任意的DateTime
对象时,用于Ruby 1.8的解决scheme:
1.8.7-p374 :001 > require 'date' => true 1.8.7-p374 :002 > DateTime.new(2012, 1, 15).strftime('%s') => "1326585600"