“Ago” date/time functions in Ruby/Rails

前端 未结 3 575
离开以前
离开以前 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:05

    distance_of_time_in_words:

    from_time = Time.now
    
    distance_of_time_in_words(from_time, from_time + 50.minutes) # => about 1 hour
    distance_of_time_in_words(from_time, 50.minutes.from_now) # => about 1 hour
    distance_of_time_in_words(from_time, from_time + 15.seconds) # => less than a minute
    distance_of_time_in_words(from_time, from_time + 15.seconds, include_seconds: true) # => less than 20 seconds
    

    time_ago_in_words:

    time_ago_in_words(3.minutes.from_now) # => 3 minutes
    time_ago_in_words(3.minutes.ago) # => 3 minutes
    time_ago_in_words(Time.now - 15.hours) # => about 15 hours
    

提交回复
热议问题