Ruby/Rails: converting a Date to a UNIX timestamp

Ruby on-RailsRubyDateTimestamp

Ruby on-Rails Problem Overview


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 Date#to_time and then getting the timestamp results in something that's off by about a month (not sure why...).

Any help is appreciated, thanks!

Edit: OK, I think I figured it out- I was processing a date several times in a loop, and each time the date was moved a little because of a time zone mismatch, ultimately leading to my timestamp being a month off. Still, I'd be interested in knowing if there's any way to do this without relying on Date#to_time.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

The code date.to_time.to_i should work fine. The Rails console session below shows an example:

>> Date.new(2009,11,26).to_time
=> Thu Nov 26 00:00:00 -0800 2009
>> Date.new(2009,11,26).to_time.to_i
=> 1259222400
>> Time.at(1259222400)
=> Thu Nov 26 00:00:00 -0800 2009

Note that the intermediate DateTime object is in local time, so the timestamp might be a several hours off from what you expect. If you want to work in UTC time, you can use the DateTime's method "to_utc".

Solution 2 - Ruby on-Rails

I get the following when I try it:

>> Date.today.to_time.to_i
=> 1259244000
>> Time.now.to_i
=> 1259275709

The difference between these two numbers is due to the fact that Date does not store the hours, minutes or seconds of the current time. Converting a Date to a Time will result in that day, midnight.

Solution 3 - Ruby on-Rails

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"

Solution 4 - Ruby on-Rails

The suggested options of using to_utc or utc to fix the local time offset does not work. For me I found using Time.utc() worked correctly and the code involves less steps:

> Time.utc(2016, 12, 25).to_i
=> 1482624000 # correct

vs

> Date.new(2016, 12, 25).to_time.utc.to_i
=> 1482584400 # incorrect

Here is what happens when you call utc after using Date....

> Date.new(2016, 12, 25).to_time
=> 2016-12-25 00:00:00 +1100 # This will use your system's time offset
> Date.new(2016, 12, 25).to_time.utc
=> 2016-12-24 13:00:00 UTC

...so clearly calling to_i is going to give the wrong timestamp.

Solution 5 - Ruby on-Rails

DateTime.new(2012, 1, 15).to_time.to_i

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionigul222View Question on Stackoverflow
Solution 1 - Ruby on-RailsDavid GraysonView Answer on Stackoverflow
Solution 2 - Ruby on-RailsRyan BiggView Answer on Stackoverflow
Solution 3 - Ruby on-RailsNowakerView Answer on Stackoverflow
Solution 4 - Ruby on-RailsGerryView Answer on Stackoverflow
Solution 5 - Ruby on-Railswang skyView Answer on Stackoverflow