2

I'm guessing I need to create a helper method to achieve this, but essentially I'd like to use distance_of_time_in_words_to_now to display the time between a datetime field's data and now, but in more specific words. For instance, if it's 3 days it should say "in 3 days". If it's tomorrow, it should say "tomorrow" (without the word "in").

All I currently have is:

<%= distance_of_time_in_words_to_now(interaction.action_date) %>

Any points in the right direction here? Thanks!

Trevan Hetzel
  • 1,371
  • 7
  • 21
  • 42
  • This could solve the problem with a little tweaking as you like: http://stackoverflow.com/questions/9779244/ruby-rails-converting-datetime-to-natural-language-i-e-3-23-2012-to-this-fri – Billy Chan May 16 '13 at 15:24

2 Answers2

0

If you check out the source of this method (http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words) you will see it uses localization. The key for days is :x_days, so in your en.yml file, you can override that, using :count, like the following:

en:
  x_days: "in %{count} days"

Now for "tomorrow", I would create a helper that either returns your custom responses or fallsback to distance_of_time_in_words, like so:

def custom_distance_of_time_in_words_to_now(time)
  return "tomorrow" if time.tomorrow? #however you decide to check
  distance_of_time_in_words_to_now(time)
end
omarvelous
  • 2,774
  • 14
  • 18
-1

You may have to add the word "days" but rest you can get from the following

Time.now.strftime("%d") #for today's date that will give you 16

Check the following out in >> irb it gives what you want

Time.now.strftime("%d")+ " days" # output => "16 days"
salah-1
  • 1,299
  • 11
  • 15