How to Say Something Happened "X Minutes Ago" or "X Hours Ago" or "X Days Ago" in Ruby

How do you say something happened x minutes ago or x hours ago or x days ago in Ruby?

If you are on rails:

time_ago_in_words

Display datetime like 1 hour ago, 5 hours ago, 3 days ago,

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words

How to show one hour later from now or one hour before now in ruby

There are no such methods in Ruby. You'll have to do:

Time.now + 1*60*60 # 1.hours.from_now -> 1 hour, 60 minutes, 60 seconds
Time.now - 1*60*60 # 1.hours.before_now -> 1 hour, 60 minutes, 60 seconds

or you can include activesupport in your project.

Ago date/time functions in Ruby/Rails

You can use:

10.minutes.ago
2.days.since

Or in your views you have the helpers:

distance_of_time_in_words(from_time, to_time)
time_ago_in_words(from_time)

Check the API for details and more options.

Ruby get days, hours and minutes from variable value

days 27 - 2332800 # would expect 27.days
hours 1 : 3600 # would expect 1.hour
minutes 13 : 780 # would expect 13.minutes

2332800 IS 27.days. Internally, all durations are represented in seconds anyway (so you can do math on them). When you use duration in the interpolation, there's an implicit .to_s call to convert it to string. Check this out:

27.days # => 27 days
27.days.to_s # => "2332800"

Rails distance_of_time_in_words returns en, about_x_hours

So I finally got it to work!! Hope this helps anyone who might have the same problem...

Basically half of what Lichtamberg first said was correct. The en.yml had to be as follows:

en:
x_minutes:
one: "1 minute"
other: "%{count} minutes"

Note that x_minutes is not under datetime and that it has one and other. Further, there is no need for I18n.l as I18n is already implemented in the distance_of_time_in_words method.

So with the above (plus all the other about_x_hours, x_days, etc patterns that you can find on the file Lichtamberg included), just do:

time_ago_in_words(3.minutes.ago)

And... voila!

want to compare a strtotime value to currenttime and output: something x min ago

Since you're using unix_timespan, it's only a matter of using basic maths :) Unix timespan is the number of seconds since January 1 1970 00:00:00 UTC. http://en.wikipedia.org/wiki/Unix_time


$postTime = $newsfeed_into_array[2];
$now = time();
$seconds = $now - $postTime; // $seconds now contains seconds since post time

$minutes = ceil($span / 60); // minutes since post time

Alternatively you can make a function that prints seconds, minutes, hours etc

Ruby Date Subtraction (e.g. 90 days Ago)

require 'date'
now = Date.today
ninety_days_ago = (now - 90)

Running this thru the IRB console I get:

>>require 'date'
now = Date.today
ninety_days_ago = (now - 90)

require 'date'
=> false
now = Date.today
=> #<Date: 2011-03-02 (4911245/2,0,2299161)>
ninety_days_ago = (now - 90)
=> #<Date: 2010-12-02 (4911065/2,0,2299161)>


If you need the time you could just say now = DateTime.now

Turn ISO 8601 date/time into x minutes ago type of string

First, parse it into a Date object. Then, make a new Date which will have the current date/time. Then, call the .getTime() methods on both of the Date objects, and subtract. Now you have your time in milliseconds.

You can do your own math from there, because until you start worrying about leap years it's all easy division.



Related Topics



Leave a reply



Submit