Search code examples
rubydatetimestampoperation

Ruby - Timestamp of yesterday to a certain time


I have the following issue: I want to get the timestamp of yesterday on a specific time.

With what I've come up so far is:

Time.local(Time.now.strftime("%Y"), Time.now.strftime("%m"), (Time.now.strftime("%d")-1), 23, 59, 59).tv_sec

I'm new to Ruby so that's probably the reason why my Ruby code looks so similar to PHP :)

Maybe it's a help when I say I would like something like this PHP solution in Ruby:

mktime(23,59,59,date("m",time()),date("d",time()-86400),date("Y",time()));

Solution

  • Is it a problem to use Activesupport?

    require 'active_support'
    Date.yesterday.end_of_day # .to_i to get the timestamp in seconds
    #=> Mon Nov 14 23:59:59 +0100 2011