Ruby: subtract two dates to get Updated at - ruby-on-rails

I am looking for a way to subtract two dates. I am not sure if a regular - would work for this. Asuming that works I still need it to show Updated 5s ago and not 0h0m5s ago if the post was updated 5s ago. The same goes for Updated 3min ago, I do not want it to show the seconds nor the hours.
So I need the updated at to be as precise as the least precise time unit that is not equal to 0.
If post was updated less than 1 sec ago show now
If post was updated < 1 min ago, but > 1 sec ago have format #s
If post was updated < 1 hour ago, but > 1 min ago have format #m
If post was updated < 1 day ago, but > 1 hour ago have format #h
If post was updated < 1 month ago, but > 1 day ago have format #d
If post was updated < 1 year ago, but > 1 month ago have format #m
If post was updated > 1 year ago have format #y
Please let me know if I wasn't clear enough.
Thank you.

try this in view
<%= time_ago_in_words(#post.updated_at) %>

check at time_ago_in_words method or distance_of_time_in_words

Related

Rails created_at format to show only minute, day, or week

I'd like to Post's to be displayed either showing how many minutes ago they were posted or weeks ago.
For example, if the post was created 5 minutes ago, it should be displayed as
5m
5 days ago:
5d
10 weeks ago:
10w
and so on.
Previously I was just using time_ago_in_words, but would prefer a simpler view.
Check out strftime, it will probably help you.
If it doesn't, take a look at to_formatted_s.
Hope it helps =]
Looks like you need to create your own solution, for example:
In your ApplicationHelper:
def short_time_ago_in_words(time)
case time
when (1.minute.ago)...(Time.now) then "1m"
when...
when (Date.today - 1)..(Date.today) then "1d"
when (Date.today - 7)..(Date.today) then "1w"
when...
end
end

show weeks_ago based off week number?

Im trying to show the number of weeks ago calculated from the week number
" x weeks ago "
this should just increment one week ago, two weeks ago, three weeks ago, 1 month ago, 2 months ago, 1 year ago.. etc
Im using method helper:
def weeks_ago_in_words(from_time, include_seconds = false)
to_time = Time.now
weeks_ago = ((to_time - from_time)/1.week).abs
[nil, "last week", "two weeks ago", "three weeks ago"][weeks_ago] ||
distance_of_time_in_words(from_time, to_time, include_seconds)
end
In view:
= weeks_ago_in_words(Time.local(2013) + week.to_i)
week = a week number like 1,10,28,52 etc
This does not give me correct resuls, is there a better way to calculate the "x weeks ago " based on the weeknumer?
If you are comparing two dates from the same year here is the code using cweek:
require 'date'
Time.now.to_date.cweek - Date.new(2013,1,1).cweek
If you are comparing two different years you'll have to add some conditional logic to determine your year multiplier as cweek returns the same value for Dec. 31, 2012 and Jan. 1, 2013.

Shoter time_ago_in_words eg. 1d ago

In my design i have a latest posts block, in this block there is very little space to say when the post was posted. Therefore, I did it as "1d" instead of "1 day ago". I would like it to be either "Today", "Yesterday" or "xd"(1d, 2d, 3d, etc)
The main reason for not using hours and minutes(and seconds..) is that my format is like this posted_date="2013-01-04", so no hours, minutes, seconds etc.
Is this posible?
def days_ago(date)
days = ((Time.now - date) / 24 / 60 / 60).round
case days
when 0 then 'Today'
when 1 then 'Yesterday'
else "#{days}d"
end
end

Humanizing time

I have a number of products that are perishable. Therefore, each product has an attribute called hours_expiration that tells how many hours the product can be used before it goes bad.
For ex, apple expires in 168 hours; nut expires in 4320 hours.
Given, the product's hours-to-expiration and the current time (Time.now or Date.now), how can I humanize the time-to-expiration in some of the following sample ways?
Your item is set to expire in about:
6 months and 14 days
1 month and 13 days
1 month and 1 day
27 days
1 day
23 hours
1 hour
50 minutes
1 minute
Looking for something robust and simple!
The distance_of_time_in_words helper seems to be what you ask for.
Another easy helper is time_ago_in_words: https://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words
The method name might sound like it can only deal with past dates but actually it handles future dates just fine. You can try it in your rails console:
expiration_date = Time.now + 5.days
puts "Expires in #{helper.time_ago_in_words(expiration_date)}"
"Expires in 5 days"
Look at Distance of time docs: http://apidock.com/rails/ActionView/Helpers/DateHelper/distance_of_time_in_words

How can I make a `weeks_ago` helper?

How can I make a helper that will tell me how many weeks ago (rounded down) with rails? It could be based of the time_ago_in_words helper however I'd want it to return: "last week" then afterwards just two weeks ago, three weeks ago, etc...
Try this:
def my_time_ago_in_words(from_time, include_seconds = false)
to_time = Time.now
weeks_ago = ((to_time - from_time)/1.week).abs
[nil, "last week", "two weeks ago", "three weeks ago"][weeks_ago] ||
distance_of_time_in_words(from_time, to_time, include_seconds)
end
This function will behave the same as time_ago_in_words. When the from_time is between 1 - 3 week ago, this will print last week, two weeks ago, three weeks ago otherwise it will print the usual.
That would be a nice patch to distance_of_time_in_words:
http://github.com/rails/rails/blob/4cbb9db0a5ff65b0a626a5b043331abefd89e717/actionpack/lib/action_view/helpers/date_helper.rb#L68-103
Also, you can write custom time descriptions for the other levels (day, month, year).
http://robots.thoughtbot.com/post/392707640/the-more-you-know-custom-time-descriptions

Resources