Rails shorter “time_ago_in_words”

前端 未结 2 897
鱼传尺愫
鱼传尺愫 2021-02-06 00:06

Is there a different time calculation in rails besides \"time_ago_in_words\"?? I want to be able to use just \'h\' for hours \'d\' days \'m\' for months... ex. 3d, or 4h, or 5m<

2条回答
  •  耶瑟儿~
    2021-02-06 00:37

    The components that make up this string can be localised, and are in the datetime.distance_in_words namespace

    For example stick

    en:
      datetime:
        distance_in_words:
          x_minutes:
            one: "1m"
            other: "%{count}m"
    

    And rails will say 10m instead of 10 minutes. Repeat as needed for hours, seconds days etc. you can check locales/en.yml in action_view for all the keys.

    If you only want the short format you could create a pseudo locale that only used those keys and use it like so

    time_ago_in_words created_at, false, :locale => :en_abbrev
    

提交回复
热议问题