计算时间间隔占 ruby/rails 中其他时间间隔的百分比

Calculate time gap as percentage of other time gap in ruby/rails

Posts 在我的网站上有一个 duration 字段,可以是一周、两周或三周,存储为整数(1、2 或 3)。我想告诉用户他们的 post 时间已经过去了多少百分比。

因此,如果它是 created_at 6 天前,并且持续时间是一周,则大约有 85% 会到期。

我试了很多东西,感觉应该很简单,但是就是想不通。

((Time.now - created_at)/duration.weeks.to_f*100).round