I need to know how to do relative time in rails but not as a sentence, more like something i could do this with (when i input format like this 2008-08-05 23:48:04 -0400)
if time_ago < 1 hour, 3 weeks, 1 day, etc.
execute me
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Basic relative time:
Comparison:
Use a
<to see iftime_agois older than1.hour.ago, and>to see iftime_agois more recent than1.hour.agoCombining times, and using fractional times:
You can combine times, as davidb mentioned, and do:
You can also do fractional seconds, like:
0.5.seconds.agoThere is no
.milliseconds.ago, so if you need millisecond precision, just break it out into a fractional second. That is, 1 millisecond ago is:.ago() in general:
Putting
.agoat the end of just about any number will treat the number as a #of seconds.You can even use fractions in paranthesis:
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.