How do you do relative time in Rails?

前端 未结 11 1578
借酒劲吻你
借酒劲吻你 2020-12-02 03:43

I\'m writing a Rails application, but can\'t seem to find how to do relative time, i.e. if given a certain Time class, it can calculate \"30 seconds ago\" or \"2 days ago\"

11条回答
  •  情歌与酒
    2020-12-02 04:29

    Take a look at the instance methods here:

    http://apidock.com/rails/Time

    This has useful methods such as yesterday, tomorrow, beginning_of_week, ago, etc.

    Examples:

    Time.now.yesterday
    Time.now.ago(2.days).end_of_day
    Time.now.next_month.beginning_of_month
    

提交回复
热议问题