Reno
Reno

Reputation: 2982

Checking if a datetime field is > than a specific hour of the current day

Rails 2.3.5 / Ruby 1.8.7

For a datetime record field, is there a Time method that would make it possible to say "if this time is > 5am this morning" in a single line?

Like:

<td>
 <% if my_data.updated_at > 5am this morning %>
   Valid
 <% else %>
  Expired
 <% end %>
</td>

I guess otherwise it woudl be storing now(), changing it's 'hour' property to '05' and then comparing the datetime field to that?

Thanks - Working with Times is still confusing to me for some reason.

Upvotes: 0

Views: 642

Answers (2)

Reno
Reno

Reputation: 2982

<td style="text-align:center;">
  <% if my_data.last_status_update.blank? %>
       &nbsp; - &nbsp;
  <% else %>
      <%=h my_data.last_status_update.strftime("%m-%d-%Y @ %H:%M CST") %>
  <% end %>
</td>
   <%
      if !my_data.last_status_update.blank? && my_data.last_status_update.year == Time.now.year &&
      my_data.last_status_update.day == Time.now.day && my_data.last_status_update.hour >= 5
   %>
       <td style="text-align:center;background:#90ee90">
         YES
       </td>
  <% else %>
        <td style="text-align:center;background:#ff9999">
         EXPIRED!
       </td>
  <% end %>

Upvotes: 2

Justin D.
Justin D.

Reputation: 4976

I have never player with the Time method, but I guess you could check the rails API : http://corelib.rubyonrails.org/classes/Time.html.

You could play with Time.at.

Upvotes: 0

Related Questions