如何在铁轨时间前显示分钟,小时或数周?

时间:2014-04-08 08:14:49

标签: ruby-on-rails

我想基于created_at得到以下结果:

1-59M

1-24H

1-999 + W

例如,如果帖子是5分钟,它会说5M。如果它是15个小时,则会说:15H,如果是52周,它会说52W

奖励:我将如何使用它:https://github.com/basecamp/local_time

2 个答案:

答案 0 :(得分:1)

我相信您使用strftime来管理i18n

根据strftimer,您需要使用%-dH%-dM%-dD%-dW来获取您想要的格式:

#view
<%=l record.created_at, format: :small %>

#config/locales/en.yml
time:
   small: %-dH

我试过测试过这个&amp;它只会带回初始数字。需要进行更多测试,但应该让您走上正确的轨道

答案 1 :(得分:1)

你只需要几周,几小时或几分钟?怎么样(它会在帮助文件中)

def short_age_string(time)
  diff = Time.now - time #value is seconds (float)
  if diff >= 0
    result = "1-"
  else
    result = "1+"
  end
  diff = diff.abs.to_i
  if diff >= 604800 #seconds in a week
    weeks = diff/604800
    return "#{result}#{weeks}#{"+" if weeks >= 999}W"
  elsif diff > 3600 #seconds in an hour
    return "#{result}#{diff/3600}H"
  else
    return "#{diff/60}#{minutes}M"
  end
end

我冒昧地让它回归&#34; 1 + ...&#34;在将来的时间。