在两个给定时间之间展示15分钟的步骤 [英] display 15-minute steps between two given times
本文介绍了在两个给定时间之间展示15分钟的步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- @ appointment.each do | form |
= form.date
= form.start_time
= form.end_time
解决方案
我有一个班轮,但这是你真正想要的吗?希望这两个日期之间的三角洲并不是太大,下面的代码不是非常有效。
(时间。 now.to_i..1.hour.from_now.to_i).to_a.in_groups_of(15.minutes).collect(&:first).collect {| t | Time.at(t)}
更好的解决方案:
#您的变量
time_start = Time.now
time_end = time_start + 1.hour
[time_start] .tap {| array |数组<< array.last + 15.minutes while array.last< time_end}
我添加了到
到Ruby的时间
实例方法:
class Time
def to(to,step = 15.minutes)
[self] .tap {| array |数组<< array.last + step while array.last< to}
end
end
所以你最终会得到这样的代码:
time_start.to(time_end)
所有三个解决方案将以包含时间
对象的数组结束。
=> [2011-07-22 17:11:11 +0200,2011-07-22 17:26:11 +0200,2011-07-22 17:41:11 +0200,2011-07-22 17:56:11 +0200,2011-07-22 18:11:11 +0200]
I have an appointments table which saves 2 times. start and end. I want to display all appointments in 15 minute steps. e.g. given 10am /11am, now display me 10.15, 10.30, 10.45 in my view! Thx for advise!
-@appointment.each do |form|
=form.date
=form.start_time
=form.end_time
解决方案
I have an one-liner, but is this what you really want? And hopefully the delta between this two dates isn't too big, the code below isn't very efficient.
(Time.now.to_i..1.hour.from_now.to_i).to_a.in_groups_of(15.minutes).collect(&:first).collect { |t| Time.at(t) }
Better solution:
# Your variables
time_start = Time.now
time_end = time_start + 1.hour
[time_start].tap { |array| array << array.last + 15.minutes while array.last < time_end }
I added to
to Ruby's Time
instance methods:
class Time
def to(to, step = 15.minutes)
[self].tap { |array| array << array.last + step while array.last < to }
end
end
so you end up with code like this:
time_start.to(time_end)
All three solutions will end in an array containing Time
objects of steps.
=> [2011-07-22 17:11:11 +0200, 2011-07-22 17:26:11 +0200, 2011-07-22 17:41:11 +0200, 2011-07-22 17:56:11 +0200, 2011-07-22 18:11:11 +0200]
这篇关于在两个给定时间之间展示15分钟的步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文