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)
Related
I have an active relation Bar object with an attribute shift_date. shift_date represents each day between March and June. March and June comes from Foo which has attributes start_month and end_month:
f = Foo.find(1)
days = (f.end_month - f.start_month).to_i
weeks = (days * 0.142857).round(2)
f.bars will give me days objects. Where days is the total amount of objetcs.
My trouble is to get Bars objects, objects for week 1, 2 or 3 etc:
f.bars.where('shift_date >= ?', (weeks/7.days)).group_by{ |result| result }
operator does not exist: timestamp without time zone >= numeric
So what am I saying? Give me all objects on week 1 or week 5, if any. How do I go about this, please?
Im on to something but not right:
f.bars.where('shift_date >= ?', Date.today).group_by{ |result| result}
Edit:
Im almost there. I could splat out the days with:
days_array = *(f.start_month..f.end_month)
then
f.bars.where(shift_date: days_array[0]..days_array[7])
That would be the answer! But...not really. For my views, I need to group the splatted days in a 7 days interval as week, so days_array[0] to days_array[7] would be week 1 and days_array[8] to days_array[14] would be week 2 etc. How to show that in the view? This will give me everything I need.
I am building a daily deal application to learn Ruby on Rails.
I'd like to display for each deal how long until the deal is no longer available in days AND hours.
I am now using in my view:
Ends in : <%= distance_of_time_in_words_to_now(game.game_end_date) %>
If there are only a few hours left, it works well and returns for example "deal ends in 3 hours".
When there is still more than ONE day to go the method returns "deal ends in about 2 days".
I would like to display something more precise in that case with the number of days AND hours. For example, it should return "deal ends in 2 days and 7 hours".
Is there a Rails method for this?
You can use the gem dotiw that has more options than the classic distance_of_time_in_words:
<%= distance_of_time_in_words(Time.now, game.game_end_date, false, :only => ["days", "hours"]) %>
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 my application I have a variety of date sequences, such as Weekly, Monthly and Annually. Given an arbitrary date in the past, I need to calculate the next future date in the sequence.
At the moment I'm using a sub-optimal loop. Here's a simplified example (in Ruby/Rails):
def calculate_next_date(from_date)
next_date = from_date
while next_date < Date.today
next_date += 1.week # (or 1.month)
end
next_date
end
Rather than perform a loop (which, although simple, is inefficient especially when given a date in the distant past) I'd like to do this with date arithmetic by calculating the number of weeks (or months, years) between the two dates, calculating the remainder and using these values to generate the next date.
Is this the right approach, or am I missing a particularly clever 'Ruby' way of solving this? Or should I just stick with my loop for the simplicity of it all?
Because you tagged this question as ruby-on-rails, I suppose you are using Rails.
ActiveSupport introduces the calculation module which provides an helpful #advance method.
date = Date.today
date.advance(:weeks => 1)
date.advance(:days => 7)
# => next week
I have used the recurrence gem in the past for this purpose. There are a few other gems that model recurring events listed here.
If you are using a Time object, you can use Time.to_a to break the time into an array (with fields representing the hour, day, month, etc), adjust the appropriate field, and pass the array back to Time.local or Time.utc to build a new Time object.
If you are using the Date class, date +/- n will give you a date n days later/earlier, and date >>/<< n will give you a date n months later/earlier.
You can use the more generic Date.step instead of your loop. For example,
from_date.step(Date.today, interval) {|d|
# Each iteration of this block will be passed a value for 'd'
# that is 'interval' days after the previous 'd'.
}
where interval is a length of time in days.
If all you are doing is calculating elapsed time, then there is probably a better approach to this. If your date is stored as a Date object, doing date - Date.today will give you the number of days between that date and now. To calculate months, years, etc, you can use something like this:
# Parameter 'old_date' must be a Date object
def months_since(old_date)
(Date.today.month + (12 * Date.today.year)) - (old_date.month + (12 * old_date.year))
end
def years_since(old_date)
Date.today.year - old_date.year
end
def days_since(old_date)
Date.today - old_date
end
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