views:

84

answers:

3

Say I have a todo app that has title and due_date fields. How do I say "Due in X days?" If a certain todo is due on Dec 29, it should say due in 2 days, X hours.

I suppose it was to do something with Time.now. I understand there's a

<%= distance_of_time_in_words_to_now%> is there an opposite to that?

Thanks

A: 

Hi,
you can try from_now

3.days.from_now  #=> Thu, 31 Dec 2009 07:51:05 UTC +00:00

Can also work with hours

2.hours.from_now #=> Mon, 28 Dec 2009 11:54:07 UTC +00:00

and the rails api page

hth
P.

Pierre
A: 

Here's the implementation of distance_of_time_in_words: http://github.com/rails/rails/blob/v2.3.5/actionpack/lib/action%5Fview/helpers/date%5Fhelper.rb#L62

Shouldn't be too hard to implement your own reversed version.

August Lilleaas
Turns out it was as easy as <%= distance_of_time_in_words(post.due, Time.now) %> Although it doesn't differentiate between past and present, i,e. 10 minutes from now and 10 minutes before now are shown simply as 10 mins.
Senthil
A: 

If anyone is interested, here is how I solved it.

<% if post.date < Time.now -%>
   Due <%=h distance_of_time_in_words(post.date, Time.now) %> ago
<% else -%>
   Due in <%= distance_of_time_in_words(post.date, Time.now) %>
<% end -%>

I'll probably make the fonts different, say red for overdue.

Remember to change timezones if you are not living in coordinated universe time (UTC).

#First find your time zone
rake time:zones:local

#My output
* UTC -08:00 *
Pacific Time (US & Canada)
Tijuana

#Copy 'Pacific Time (US & Canada)' and replace line 46 in config/environment.rb
config.time_zone = 'Pacific Time (US & Canada)'

#Be sure to restart your server and you are set to go.
Senthil