“Ago” date/time functions in Ruby/Rails

前端 未结 3 579
离开以前
离开以前 2021-01-30 19:23

I was wondering if there\'s a way in Rails to calculate time stamp like - half a minute ago, 2 minute ago, 1 day ago etc. Something like twitter real time date stamp.

I

3条回答
  •  故里飘歌
    2021-01-30 20:01

    You can use available methods to get the time in past or future using ago, since alias for from_now and many available methods

    Time.current
    #=> Tue, 20 Sep 2016 15:03:30 UTC +00:00
    
    2.minutes.ago
    #=> Tue, 20 Sep 2016 15:01:30 UTC +00:00
    
    2.minutes.since
    #=> Tue, 20 Sep 2016 15:05:30 UTC +00:00 
    
    1.month.ago
    #=> Sat, 20 Aug 2016 15:03:30 UTC +00:00
    
    1.year.since
    #=> Wed, 20 Sep 2017 15:03:30 UTC +00:00 
    

    Check all the available methods in Time class

提交回复
热议问题