rails

时间:2018-05-24 17:29:00

标签: ruby-on-rails date

我正在创建一个新的对话应用,其中会话消息在提升日期显示,我已经这样做了:

messages = conversation.messages.group_by { |message|
  message.created_at.strftime('%m%d%Y')
}

我正在显示带有此键的消息(分组日期)。如何在今天和昨天显示今天和昨天的消息?

1 个答案:

答案 0 :(得分:1)

不确定TodayTomorrow但您可以使用distance_of_time_in_words方法显示何时创建邮件。

2.3.1 :031 > require 'action_view'
 => false 
2.3.1 :032 > include ActionView::Helpers::DateHelper
 => Object 
2.3.1 :033 > distance_of_time_in_words(Time.now, Time.now) + ' ago'
 => "less than a minute ago" 
2.3.1 :034 > distance_of_time_in_words(Time.now, Time.now - 1.hour) + ' ago'
 => "about 1 hour ago" 
2.3.1 :035 > distance_of_time_in_words(Time.now, Time.now - 90.minutes) + ' ago'
 => "about 2 hours ago" 
2.3.1 :036 > distance_of_time_in_words(Time.now, Time.now - 1.day) + ' ago'
 => "1 day ago" 
2.3.1 :037 > distance_of_time_in_words(Time.now, Time.now - 5.day) + ' ago'
 => "5 days ago" 

你也可以使用像dotiw

这样的宝石