3.days.ago, 2.hours.from_now etc without Rails?

蓝咒 提交于 2019-12-03 09:29:24
Michael Kohl

I'm not sure if there's another gem available besides ActiveSupport, but it would be really straight-forward to make a small version yourself:

class Fixnum
  SECONDS_IN_DAY = 24 * 60 * 60

  def days
    self * SECONDS_IN_DAY
  end

  def ago
    Time.now - self
  end
end

3.days.ago #=> 2011-06-18 08:45:29 0200

from_now can be implemented like ago but with + self and weeks, hours etc. like days using different constants.

ActiveSupport has this functionality. It was originally part of Rails but can now be used separately.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!