Ruby/Rails: converting a Date to a UNIX timestamp

后端 未结 5 1884
迷失自我
迷失自我 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条回答
  •  -上瘾入骨i
    2020-12-07 08:42

    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.

提交回复
热议问题