Skip to content

Working with time in Ruby

Melvin Lau edited this page Nov 8, 2019 · 3 revisions

Date/time formatting

Useful website to choose your format and get the required string: http://strftimer.com/

Getting the time difference

For this project, the client specified that a user can update their own posts for a maximum of 10 minutes after they're created.

To implement this, we render the Edit button on a post only if it has been 10 minutes or less since the post was created.

<%= button_to "Edit", edit_post_path(post.id), :method => :get if (Time.now - post.created_at) / 60 <= 10 %>

Time.now - post.created_at gives the time difference in seconds.