Convert Ruby Date to Integer

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)