开发者

How to do "Due in X days/time" for a todo app in rails?

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

开发者_如何学Python

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

Thanks


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.


Here's the implementation of distance_of_time_in_words: http://github.com/rails/rails/blob/v2.3.5/actionpack/lib/action_view/helpers/date_helper.rb#L62

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


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


Here is ported version

http://gaskell.org/rails-helper-distance_of_time_in_words-ported-to-c/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜