Ruby/Rails: converting a Date to a UNIX timestamp

后端 未结 5 1888
迷失自我
迷失自我 2020-12-07 08:12

How would I get a UNIX timestamp (number of seconds since 1970 GMT) from a Date object in a Rails app?

I know Time#to_i returns a timestamp, but doing <

5条回答
  •  温柔的废话
    2020-12-07 08:36

    Solution for Ruby 1.8 when you have an arbitrary DateTime object:

    1.8.7-p374 :001 > require 'date'
     => true 
    1.8.7-p374 :002 > DateTime.new(2012, 1, 15).strftime('%s')
     => "1326585600"
    

提交回复
热议问题