Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:50:25+00:00 2026-06-03T07:50:25+00:00

I am looking for a helper class/method/gem that’s out there that will help me

  • 0

I am looking for a helper class/method/gem that’s out there that will help me format a time helper. The output I’m looking after passing in an instance of Time.now is something like the following:

"1 minute ago" 
"2 minutes ago"
"1 hour ago"
"2 hours ago"
"1 day ago"
"2 days ago"
"over a year ago"

I started writing something like this but it’s going to be long and painful and I feel like something like this has to exist. The only catch is I need it to use my own wording, so something with a formatter is required..

 def time_ago_to_str(timestamp)
    minutes = (((Time.now.to_i - timestamp).abs)/60).round
    return nil if minutes < 0
    Rails.logger.debug("minutes #{minutes}")

    return "#{minutes} minute ago" if minutes == 1
    return "#{minutes} minutes ago" if minutes < 60
    # crap load more return statements to follow?
  end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:50:26+00:00Added an answer on June 3, 2026 at 7:50 am

    Such a helper already exists and is built-in to Rails:

    http://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words

    time_ago_in_words(5.days.ago)
    => "5 days"
    

    EDIT:

    If you’d like to customize the wording you can create a custom I18n locale, for example, I created one called time_ago in config/locales/time_ago.yml:

    time_ago:
      datetime:
         distance_in_words:
           half_a_minute: "half a minute"
           less_than_x_seconds:
             one:   "less than 1 second"
             other: "less than %{count} seconds"
           x_seconds:
             one:   "1 second"
             other: "%{count} seconds"
           less_than_x_minutes:
             one:   "less than a minute"
             other: "less than %{count} minutes"
           x_minutes:
             one:   "1 min"
             other: "%{count} mins"
           about_x_hours:
             one:   "about 1 hour"
             other: "about %{count} hours"
           x_days:
             one:   "1 day"
             other: "%{count} days"
           about_x_months:
             one:   "about 1 month"
             other: "about %{count} months"
           x_months:
             one:   "1 month"
             other: "%{count} months"
           about_x_years:
             one:   "about 1 year"
             other: "about %{count} years"
           over_x_years:
             one:   "over 1 year"
             other: "over %{count} years"
           almost_x_years:
             one:   "almost 1 year"
             other: "almost %{count} years"
    

    Now, you can use the locale with distance_of_time_in_words:

    # distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, options = {})
    distance_of_time_in_words(5.minutes.ago, Time.now, true, {:locale => "time_ago"})
     => "5 mins" 
    

    You could of course add this to config/locales/en.yml and completely override them application wide, which would you allow you to call time_ago_in_words as mentioned above!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an I18n helper class that can find out the available Locale s
I am looking for a helper tool/ script that can be used to power
My helper class has a plugin-dependecy and I'd like to double check if that
I'm looking for a way to define a method that returns a type T
I'm looking at creating a helper method to set an exception's message, automatically setting
I'm looking through some old code and realize there are a ton of helper
I'm looking at an old helper method which I've been using for a while
I've created a helper method in ApplicationController that allows access to the most recent
I am looking to write a few helpers in my own assembly modeled after
I've tried looking at similar problems, but could not easily find one that helped

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.