【问题标题】:Rails created_at format to show only minute, day, or weekRails created_at 格式只显示分钟、天或周
【发布时间】:2014-01-02 18:27:13
【问题描述】:

我想显示发布的时间,显示它们是在几分钟前或几周前发布的。

例如,如果帖子是 5 分钟前创建的,则应显示为

5m

5 天前:

5d

10 周前:

10w

等等。

以前我只是使用time_ago_in_words,但更喜欢更简单的视图。

【问题讨论】:

  • 我在这里也有类似的答案:stackoverflow.com/a/20853768/1721198 可能会有所帮助。
  • 您希望如何显示 5994 分钟?
  • 我猜应该是 4d,好电话虽然我会补充问题
  • 一种常见的方式是显示最多 59 分钟,之后最多 23 小时,之后最多 6-30 天,之后 8 个 52 周或 11 个月,之后以年为单位。

标签: ruby-on-rails ruby ruby-on-rails-4 strftime


【解决方案1】:

查看strftime,它可能会对您有所帮助。

如果没有,请查看to_formatted_s

希望对你有帮助 =]

【讨论】:

    【解决方案2】:

    看起来您需要创建自己的解决方案,例如:

    在你的ApplicationHelper:

    def short_time_ago_in_words(time)
      case time
      when (1.minute.ago)...(Time.now) then "1m"
      when...
      when (Date.today - 1)..(Date.today) then "1d"
      when (Date.today - 7)..(Date.today) then "1w"
      when...
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2017-06-12
      相关资源
      最近更新 更多