是否有红宝石的宝石,将格式化相对于当前时间的日期?我想要输出如“明天下午5点”,“下周四下午5:15”,我不太关心确切的输出,只要它是自然语言的相对日期即可格式相对日期
Q
格式相对日期
5
A
回答
7
如果您有导轨,我认为ActionView::Helpers::DateHelper#distance_of_time_in_words有助于这一点。
require 'rubygems'
require 'action_view'
include ActionView::Helpers::DateHelper
from_time = Time.now
distance_of_time_in_words(from_time, from_time + 50.minutes) # => about 1 hour
distance_of_time_in_words(from_time, 50.minutes.from_now) # => about 1 hour
distance_of_time_in_words(from_time, from_time + 15.seconds) # => less than a minute
distance_of_time_in_words(from_time, from_time + 15.seconds, true) # => less than 20 seconds
distance_of_time_in_words(from_time, 3.years.from_now) # => over 3 years
distance_of_time_in_words(from_time, from_time + 60.hours) # => about 3 days
distance_of_time_in_words(from_time, from_time + 45.seconds, true) # => less than a minute
distance_of_time_in_words(from_time, from_time - 45.seconds, true) # => less than a minute
distance_of_time_in_words(from_time, 76.seconds.from_now) # => 1 minute
distance_of_time_in_words(from_time, from_time + 1.year + 3.days) # => about 1 year
distance_of_time_in_words(from_time, from_time + 4.years + 9.days + 30.minutes + 5.seconds) # => over 4 years
to_time = Time.now + 6.years + 19.days
distance_of_time_in_words(from_time, to_time, true) # => over 6 years
distance_of_time_in_words(to_time, from_time, true) # => over 6 years
distance_of_time_in_words(Time.now, Time.now) # => less than a minute
在相对于当前时间的情况下,使用distance_of_time_in_words_to_now
代替distance_of_time_in_words
。
如果您的应用程序是基于导轨的,则只需使用distance_of_time_in_words
,distance_of_time_in_words_to_now
,time_ago_in_words
即可。
-3
尝试宝石“慢性”
它完全符合你的要求。
+1
慢性似乎反其道而行之:它分析时间,它不格式化。 – 2011-12-20 20:12:33
相关问题
- 1. 将wordpress中的日期格式更改为相对日期
- 2. 新格式日期对象
- 3. 更新日期列和格式日期到相同模式
- 4. 相对日期
- 5. 月份日格式的日期对象
- 6. 日期(“8601格式日期”)?
- 7. 日期格式为日期
- 8. 日期格式日期
- 9. Python中许多格式相同的日期的猜测日期格式
- 10. 日期格式
- 11. 格式日期
- 12. 日期格式
- 13. 日期格式
- 14. 日期格式
- 15. 日期格式?
- 16. 格式日期
- 17. 日期格式
- 18. 格式日期
- 19. 格式日期
- 20. 日期格式
- 21. 日期格式
- 22. 格式日期格式php
- 23. 相似的日期格式会产生意外的日期Javascript
- 24. 相对于日期
- 25. 在Android中显示与自定义格式的相对日期
- 26. Informatica日期格式
- 27. 日期格式Wikimedia
- 28. Cumulocity日期格式
- 29. 日期格式化
- 30. Oracle - 日期格式
正是我所需要的,但没有使用的轨道:(任何想法,如果滑轨组件由轨道外可用? – 2010-04-20 03:48:42
我认为上面的代码将作品没有护栏,安装动作包之后(创业板安装ActionPack的),但不。确保 – unsouled 2010-04-20 04:47:09
@unsouled - 确认 – steenslag 2010-04-20 07:07:04