漂亮日期'周前'不能正常工作

时间:2013-06-28 17:58:10

标签: ruby sinatra

我正在使用以下代码来制作与现在相关的可读时间。它工作正常,直到它需要打印'周前'代码然后Sinatra吐出:“未定义的方法`/'为”15“:字符串”

def pretty_date(stamp)
  now = Time.new
  diff = now - stamp
  day_diff = ((now - stamp) / 86400).floor

  day_diff == 0 && (
    diff < 60 && "just now" ||
    diff < 120 && "1 minute ago" ||
    diff < 3600 && (diff / 60).floor.to_s + " minutes ago" ||
    diff < 7200 && "1 hour ago" ||
    diff < 86400 && (diff/3600).floor.to_s + " hours ago") ||
  day_diff == 1 && "Yesterday" ||
  day_diff < 7 && day_diff.to_s + " days ago" ||
  day_diff < 31 && (day_diff.to_s / 7).ceil + " weeks ago";
end

1 个答案:

答案 0 :(得分:1)

在这里,您正在执行以下操作

day_diff.to_s / 7

你不能分割字符串。只是做:

day_diff / 7