在ruby中格式化日期/时间

时间:2013-04-19 05:50:14

标签: ruby ruby-on-rails-3

我收到了这种格式的日期和时间

2013-04-19 11:07:37 +0530

我想知道如何在ruby中以这种方式显示XYZ hours ago

由于

更新:我尝试了time_ago_in_words' and it gives me the error未定义的方法time_ago_in_words'

3 个答案:

答案 0 :(得分:3)

它是一种铁轨方法。您可以rails c进入rails控制台并传递include ActionView::Helpers::DateHelper。然后解决像

这样的事情
t = Time.parse("2013-04-19 11:07:37 +0530")
time_ago_in_words(t)

HTH

答案 1 :(得分:2)

使用此功能

time_ago_in_words(from_time, include_seconds = false, options = {})

参考链接:ClickHere

irb> time = "2013-04-19 11:07:37 +0530"
=> "2013-04-19 11:07:37 +0530"
irb> helper.time_ago_in_words(time)
=> "about 3 hours"

或另一种方式: - 包括ActionView :: Helpers :: DateHelper:

>> time_ago_in_words("2013-04-19 11:07:37 +0530")
NoMethodError: undefined method `time_ago_in_words' for #<Object:0x3b0454>
from (irb):4
>> include ActionView::Helpers::DateHelper
=> Object
>> time_ago_in_words("2013-04-19 11:07:37 +0530")
=> "about 3 hours"

答案 2 :(得分:1)

time_ago_in_words和distance_of_time_in_words是Rails方法。您必须定义自己的方法才能执行此操作。可能你可以从rails中复制代码。

相关问题