-1

我正在使用下面的代码来制作与现在相关的可读时间。它工作正常,直到它需要打印“几周前”代码然后 Sinatra 吐出:“undefined method `/' for "15":String"

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
4

1 回答 1

1

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

day_diff.to_s / 7

你不能分割一个字符串。做就是了:

day_diff / 7
于 2013-06-28T18:00:05.560 回答