DEV Community

Cover image for "Time in Words" Helper for Ruby on Rails
Rixio Barrios
Rixio Barrios

Posted on

"Time in Words" Helper for Ruby on Rails

One clever way to format time using Ruby is by using a Rails method called "time_ago_in_words". This method translates the hard to read default format for a date and time, making it more human friendly. Here is a quick example:

time_ago_in_words(Time.now)  # => less than a minute
Enter fullscreen mode Exit fullscreen mode

Here is how to use it in a controller:

class UsersController
  def index
    helpers.time_ago_in_words(Time.now)
  end
end
Enter fullscreen mode Exit fullscreen mode

Top comments (0)