Search code examples
ruby-on-railsdatetimeruby-on-rails-5

get 11 pm of every day as datetime


How we can set datetime which gives same time but different date on each day.

I want to get all objects which are created after 23:00:00 of each day

I have used string interpolation but can this be done in better way.

 2.5.1 :013 >     created_after = Date.today.strftime("%F") + " 23:00:00"
 => "2020-02-12 23:00:00" 

Solution

  • You can try this -

    created_after = DateTime.now.in_time_zone(Time.zone).beginning_of_day - 1.hour
    

    or

    created_after = DateTime.current.beginning_of_day - 1.hour