How do I use relative time in Rails?
This is the documentation for time_ago_in_days
and here's a few examples.
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
time_ago_in_words(Time.now) # => less than a minute
time_ago_in_words(Time.now, include_seconds: true) # => less than 5 seconds
from_time = Time.now - 3.days - 14.minutes - 25.seconds
time_ago_in_words(from_time) # => 3 days
from_time = (3.days + 14.minutes + 25.seconds).ago
time_ago_in_words(from_time) # => 3 days
This is a view helper, so it is expected to be called in a view context. It's not gonna work in your model or controller by default.
If you need the same feature elsewhere, for example in a custom class in /lib
, then you may want to check the source code and recreate the feature in your code (you can copy/paste the method, but it's likely you will need a less generic method so you may end up with a much smaller implementation).
Using and comparing relative time in Rails
Basic relative time:
# If "time_ago" is more than 1 hour past the current time
if time_ago < 1.hour.ago
execute_me
end
Comparison:
Use a <
to see if time_ago
is older than 1.hour.ago
, and >
to see if time_ago
is more recent than 1.hour.ago
Combining times, and using fractional times:
You can combine times, as davidb mentioned, and do:
(1.day + 3.hours + 2500.seconds).ago
You can also do fractional seconds, like:0.5.seconds.ago
There is no .milliseconds.ago
, so if you need millisecond precision, just break it out into a fractional second. That is, 1 millisecond ago is:
0.001.seconds.ago
.ago() in general:
Putting .ago
at the end of just about any number will treat the number as a #of seconds.
You can even use fractions in paranthesis:
(1/2.0).hour.ago # half hour ago
(1/4.0).year.ago # quarter year ago
NOTE: to use fractions, either the numerator or denominator needs to be a floating point number, otherwise Ruby will automatically cast the answer to an integer, and throw off your math.
Ruby on Rails- Time Condition
To add to the earlier answer
For posts this week
@posts = Post.where("created_at > ?", Time.now.beginning_of_week)
For posts this month:
@posts = Post.where("created_at > ?", Time.now.beginning_of_month)
How to just show minutes or hours or weeks in rails time ago?
You just want it in weeks, hours or minutes? How about this (it would go in a helpers file)
def short_age_string(time)
diff = Time.now - time #value is seconds (float)
if diff >= 0
result = "1-"
else
result = "1+"
end
diff = diff.abs.to_i
if diff >= 604800 #seconds in a week
weeks = diff/604800
return "#{result}#{weeks}#{"+" if weeks >= 999}W"
elsif diff > 3600 #seconds in an hour
return "#{result}#{diff/3600}H"
else
return "#{diff/60}#{minutes}M"
end
end
I took the liberty of making it return "1+..." for times in the future.
Format relative dates
if you have rails, I think ActionView::Helpers::DateHelper#distance_of_time_in_words helps that.
require 'rubygems'
require 'action_view'
include ActionView::Helpers::DateHelper
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, true) # => less than 20 seconds
distance_of_time_in_words(from_time, 3.years.from_now) # => over 3 years
distance_of_time_in_words(from_time, from_time + 60.hours) # => about 3 days
distance_of_time_in_words(from_time, from_time + 45.seconds, true) # => less than a minute
distance_of_time_in_words(from_time, from_time - 45.seconds, true) # => less than a minute
distance_of_time_in_words(from_time, 76.seconds.from_now) # => 1 minute
distance_of_time_in_words(from_time, from_time + 1.year + 3.days) # => about 1 year
distance_of_time_in_words(from_time, from_time + 4.years + 9.days + 30.minutes + 5.seconds) # => over 4 years
to_time = Time.now + 6.years + 19.days
distance_of_time_in_words(from_time, to_time, true) # => over 6 years
distance_of_time_in_words(to_time, from_time, true) # => over 6 years
distance_of_time_in_words(Time.now, Time.now) # => less than a minute
In case of relative to the current time, use distance_of_time_in_words_to_now
instead of distance_of_time_in_words
.
If your app is rails-based, just use distance_of_time_in_words
, distance_of_time_in_words_to_now
, time_ago_in_words
in view.
Related Topics
Can't Install Pg Gem on Windows
Ruby Gem For Finding Timezone of Location
Long Running Delayed_Job Jobs Stay Locked After a Restart on Heroku
How to Define Action With Simple Form For
How to Count Duplicates in Ruby Arrays
How to Run All Tests With Minitest
Rake "Already Initialized Constant Wfkv_" Warning
"Gem Install Rails" Fails With Dns Error
Ruby Run Shell Command in a Specific Directory
Convert Unicode Codepoint to String Character in Ruby
Validation Failed: Upload File Has an Extension That Does Not Match Its Contents
Total Newbie: Instance Variables in Ruby
Why Do Two Strings Separated by Space Concatenate in Ruby