0

我试图用 20.00、20.10、20.20 ... 24:00 之类的时间填充集合。所以每隔10分钟。但是如何巧妙地做到这一点并考虑到 Time.now 呢?

仅应列出 > Time.now 的时间。所以如果它的 20.30 它不应该显示20.10, 20.20,20.30

示例代码

= f.input :order,   :collection => ["20:00","20:10","20:20"... etc ["24:00"],
                    :default => 2,
                    :label => "orders,
                    :hint => "Select the time you want this order to be processed"

到目前为止我尝试过的一些事情:

:collection => [(Time.now + 10.minutes).strftime("%I:%M%p").to_s]

#hours=(Time.now.minus_with_coercion(Time.now.midnight)/3600/2)

有什么想法如何干净地编码吗?谢谢

4

1 回答 1

1

不确定是否理解您的问题,但这可能会有所帮助:

Time.parse('20:00').to_datetime.step(Time.parse('23:59'), 10.minutes).to_a.map {|date| date.strftime("%I:%M%p")}
=> ["08:00PM", "08:10PM", "08:20PM", "08:30PM", "08:40PM", "08:50PM", "09:00PM", "09:10PM", "09:20PM", "09:30PM", "09:40PM", "09:50PM", "10:00PM", "10:10PM", "10:20PM", "10:30PM", "10:40PM", "10:50PM", "11:00PM", "11:10PM", "11:20PM", "11:30PM", "11:40PM", "11:50PM"]

之后,您可以调用 delete_if 方法来删​​除不需要的时间。

类似的东西:

Time.parse('20:00').to_datetime.step(Time.parse('23:59'), 10.minutes).to_a.delete_if {|date| date < DateTime.now.to_time}.map {|date| date.strftime("%I:%M%p")}
于 2013-01-18T12:54:17.660 回答