2

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

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

我想知道如何XYZ hours ago在红宝石中以这种方式显示它。

谢谢

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

4

3 回答 3

4

它是一种导轨方法。rails c您可以通过并通过进入 Rails 控制台include ActionView::Helpers::DateHelper。然后解决类似的问题

t = Time.parse("2013-04-19 11:07:37 +0530")
time_ago_in_words(t)

高温高压

于 2013-04-19T05:58:24.257 回答
2

使用此功能

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

参考链接:点击这里

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"
于 2013-04-19T05:53:31.207 回答
1

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

于 2013-04-19T05:58:40.340 回答