0

Im trying to populate a collection with Times like 20.00, 20.10, 20.20 ... 24:00. So in intervals of 10.minutes. But how to do this smartly and take into account the Time.now?

Only times that are > Time.now should be listed. So if its 20.30 It should not show 20.10, 20.20,20.30

Example code

= 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"

Some of the things Ive tried so far:

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

and

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

Any thoughts how to cleanly code this ? Thank you

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 に答える