我有一个预约表,可以节省两次。开始和结束。我想在 15 分钟内显示所有约会。例如给定上午 10 点/上午 11 点,现在在我的视图中显示 10.15、10.30、10.45!谢谢您的建议!
[email protected] /cdn-cgi/l/email-protection do |form|
=form.date
=form.start_time
=form.end_time
我有一句俏皮话,但这真的是你想要的吗?希望这两个日期之间的差异不会太大,下面的代码效率不是很高。
(Time.now.to_i..1.hour.from_now.to_i).to_a.in_groups_of(15.minutes).collect(&:first).collect { |t| Time.at(t) }
更好的解决方案:
# 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
到鲁比的Time
实例方法:
class Time
def to(to, step = 15.minutes)
[self].tap { |array| array << array.last + step while array.last < to }
end
end
所以你最终会得到这样的代码:
time_start.to(time_end)
所有三个解决方案都将以包含以下内容的数组结束Time
步骤的对象。
=> [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]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)