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
Related
I need to filter from B to K IF column A is any time in the last 15 minutes from now AND column J says "Yes"
A has the format Hour-Minute-Second, for example "17:20:59". Lets say now is 18:00:00, I want this formula to return all values from 17:45:00 until now.
For now I did this:
=FILTER(B:K, A:A=TODAY(),J:J="Yes")
I need the same but instead of today, the last 15 minutes.
How can I achieve this?
Now returns the time whereas today does not, just subtract minutes / 1440 because that is how many minutes are in a day:
=FILTER(B:K, A:A>=Now()-15/1440+n(whatthefoxsay()),J:J="Yes")
So i want item to display to remaining on Items after 7 days the item will be deleted. ive tried
<%= distance_of_time_in_words(item.created_at, item.created_at + 7.days) %>
but all i get is "7 Days" on all items. Can anyone simply how this helper method works ?
Lets looks at the documentation to see what distance_of_time_in_words does:
distance_of_time_in_words(from_time, to_time = 0, options = {})
Reports the approximate distance in time between two Time, Date or DateTime objects or integers as seconds.
So it reports the time difference of the first argument and the second argument. Now, you're doing:
distance_of_time_in_words(item.created_at, item.created_at + 7.days)
The difference between item.created_at and item.created_at plus seven days is always ... seven days ;-)
I assume that this is something that will always be deleted after seven days? In that case, what you want, is the difference between the current date and the creation date plus seven days, which you can get with:
distance_of_time_in_words(Time.now, item.created_at + 7.days)
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
I'm trying to generate a Time object initialized to the next instance of a specific weekday and number of minutes past midnight.
My data (representing a schedule) looks like: weekday, start, finish. Weekday is the day of the week between 0 and 6, and start/finish are integers representing minutes past midnight on that particular weekday.
What I'd like to be able to do is get a Time object for the next time that this will be so that I can work more flexibly with the start/end times for frontend purposes.
Does anyone know how to do this? I've tinkered with Time.utc without much success.
Not sure if I'm interpreting this correctly... Is it where given a weekday, you will find the next available date that is on that weekday? For example, if today is tuesday, and your schedule is set for thursday, then the time object will be at midnight on this thursday? If that's the case, something like this should work:
day_difference = #weekday - Time.now.wday
# If the difference is in the past, then add 7 so that it is next week.
if day_difference < 0
day_difference = day_difference + 7
end
next_date = Time.now.midnight + day_difference.days
start_time = next_date + #start.minutes
end_time = next_date + #finish.minutes
Where weekday is the value in your Schedule object. Once again, I'm not 100% sure what you're asking. Here start_time and end_time are time objects representing the next weekday with the corresponding amount of minutes added to them.
You can do something basic along these lines for this with something like the following:
class Schedule
attr_accessor :day_of_week, :start_minute, :finish_minute
def start(current_date = Time.zone.now)
current_date.beginning_of_week + day_of_week.days + start_minute.minutes
end
def finish(current_date = Time.zone.now)
current_date.beginning_of_week + day_of_week.days + finish_minute.minutes
end
def length
(finish_minute - start_minute).minutes
end
end
Since that's pretty rudimentary and I'm not sure exactly what you're looking to use it for, I suggest looking at some of the libraries mentioned in this post ice_cube, and business_time look like they're applicable for what you might be wanting
In a Rails project I want to find the difference between two dates and then display it in natural language. Something like
>> (date1 - date2).to_natural_language
"3 years, 2 months, 1 week, 6 days"
Basically this for ruby.
Google and the Rails API haven't turned up anything. I've found some things that will give you the difference in one unit (ie, how many weeks between two dates) but not something that will accurately calculate years, months, weeks, days all together.
The Rails' ActionView module includes two methods that may do what you require:
distance_of_time_in_words
distance_of_time_in_words_to_now
The other answers may not give the type of output that you're looking for, because instead of giving a string of years, months, etc., the Rails helpers just show the largest unit. If you're looking for something more broken down, here's another option. Stick this method into a helper:
def time_diff_in_natural_language(from_time, to_time)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
distance_in_seconds = ((to_time - from_time).abs).round
components = []
%w(year month week day).each do |interval|
# For each interval type, if the amount of time remaining is greater than
# one unit, calculate how many units fit into the remaining time.
if distance_in_seconds >= 1.send(interval)
delta = (distance_in_seconds / 1.send(interval)).floor
distance_in_seconds -= delta.send(interval)
components << pluralize(delta, interval)
# if above line give pain. try below one
# components << interval.pluralize(delta)
end
end
components.join(", ")
end
And then in a view you can say something like:
<%= time_diff_in_natural_language(Time.now, 2.5.years.ago) %>
=> 2 years, 6 months, 2 days
The given method only goes down to days, but can be easily extended to add in smaller units if desired.
I tried Daniel's solution and found some incorrect results for a few test cases, due to the fact that it doesn't correctly handle the variable number of days found in months:
> 30.days < 1.month
=> false
So, for example:> d1 = DateTime.civil(2011,4,4)
> d2 = d1 + 1.year + 5.months
> time_diff_in_natural_language(d1,d2)
=> "1 year, 5 months, 3 days"
The following will give you the correct number of {years,months,days,hours,minutes,seconds}:
def time_diff(from_time, to_time)
%w(year month day hour minute second).map do |interval|
distance_in_seconds = (to_time.to_i - from_time.to_i).round(1)
delta = (distance_in_seconds / 1.send(interval)).floor
delta -= 1 if from_time + delta.send(interval) > to_time
from_time += delta.send(interval)
delta
end
end
> time_diff(d1,d2)
=> [1, 5, 0, 0, 0, 0]
distance_of_time_in_words is the most accurate here. Daniel's answer is actully wrong: 2.5 years ago should produce exactly 2 years, 6 months. The issue is that months contain 28-31 day, and years might be leap.
I wish I knew how to fix this :(
DateHelper#distance_of_time_in_words
def date_diff_in_natural_language(date_from, date_to)
components = []
%w(years months days).each do |interval_name|
interval = 1.send(interval_name)
count_intervals = 0
while date_from + interval <= date_to
date_from += interval
count_intervals += 1
end
components << pluralize(count_intervals, interval_name) if count_intervals > 0
end
components.join(', ')
end