用一系列增量时间以及附加选项填充选择

发布于 2024-08-21 13:54:16 字数 327 浏览 2 评论 0原文

使用 Ruby on Rails,我使用给定增量(例如每 30 分钟)填充一次“选择”。 目前我正在 YAML 文件中写出所有可能性,但我觉得有一种更灵活的方法。我想我想提供一个开始时间、一个结束时间、一个增量,目前只有一个名为“关闭”的选项(想想“营业时间”)。 所以,我的选择可能会显示:

“已关闭”

上午 5:00

上午 5:30

上午 6:00

...

[一直到]...

晚上11点30分

谁能想到更好的方法来做到这一点,或者只是将它们全部“拼写”出来是最好的方法?

Using Ruby on Rails, I'm filling a 'select' with times using a given increment, such as every 30 minutes.
Currently I'm writing out all the possibilities in a YAML file, but i feel like there's a slicker way. I'm thinking I want to provide a start_time, an end_time, an increment, and currently just one more option called 'Closed' (think 'business_hours').
So, my select might display:

'Closed'

5:00am

5:30am

6:00am

...

[all the way to]...

11:30pm

Can anyone think of a better way of doing this, or is just 'spelling' them all out the best way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷夜 2024-08-28 13:54:16

这个答案基于@emh 的答案。

def create_hours(parameters)
  start_time = parameters[:start_time] ? parameters[:start_time] : 0
  end_time = parameters[:end_time] ? parameters[:end_time] : 24.hours
  increment = parameters[:increment] ? parameters[:increment] : 30.minutes
  Array.new(1 + (end_time - start_time)/increment) do |i|
    (Time.now.midnight + (i*increment) + start_time).strftime("%I:%M %p")
  end
end

您可以这样使用它:

create_hours(:start_time => 5.hours, :end_time => 23.hours)
=> ["05:30 AM", "06:00 AM", "06:30 AM", "07:00 AM", "07:30 AM", "08:00 AM", 
    "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", 
    "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", 
    "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", 
    "05:30 PM", "06:00 PM", "06:30 PM", "07:00 PM", "07:30 PM", "08:00 PM", 
    "08:30 PM", "09:00 PM", "09:30 PM", "10:00 PM", "10:30 PM", "11:00 PM"]

要添加“关闭”使用:

["closed"] + create_hours(:start_time => 5.hours, :end_time => 23.hours)

您还可以传递 :increment =>; 15.分钟 - 默认情况下设置为30.分钟

This answer is based on @emh answer.

def create_hours(parameters)
  start_time = parameters[:start_time] ? parameters[:start_time] : 0
  end_time = parameters[:end_time] ? parameters[:end_time] : 24.hours
  increment = parameters[:increment] ? parameters[:increment] : 30.minutes
  Array.new(1 + (end_time - start_time)/increment) do |i|
    (Time.now.midnight + (i*increment) + start_time).strftime("%I:%M %p")
  end
end

You can use it this way:

create_hours(:start_time => 5.hours, :end_time => 23.hours)
=> ["05:30 AM", "06:00 AM", "06:30 AM", "07:00 AM", "07:30 AM", "08:00 AM", 
    "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", 
    "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", 
    "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", 
    "05:30 PM", "06:00 PM", "06:30 PM", "07:00 PM", "07:30 PM", "08:00 PM", 
    "08:30 PM", "09:00 PM", "09:30 PM", "10:00 PM", "10:30 PM", "11:00 PM"]

To add "Closed" use:

["closed"] + create_hours(:start_time => 5.hours, :end_time => 23.hours)

You can also pass :increment => 15.minutes - by default it is set to 30.minutes.

黑凤梨 2024-08-28 13:54:16
["closed"] + Array.new(24.hours / 30.minutes) do |i|
  (Time.now.midnight + (i*30.minutes)).strftime("%I:%M %p")
end
["closed"] + Array.new(24.hours / 30.minutes) do |i|
  (Time.now.midnight + (i*30.minutes)).strftime("%I:%M %p")
end
墨离汐 2024-08-28 13:54:16

您可以考虑使用 HTML5 time 输入类型来代替

<input type='time' min='05:30' max='23:30' step='00:30' />

由于 time 输入仅在 HTML5 中可用(事实上,到目前为止只有 Opera 支持它),您将需要一种让其他浏览器使用它的方法。我问了一个关于为 元素。

Instead of a <select> you might consider using the HTML5 time input type:

<input type='time' min='05:30' max='23:30' step='00:30' />

Since the time input is only available in HTML5 (indeed, only Opera supports it so far), you'll want a way for other browsers to use it. I asked a question about doing the same thing for a <input type='range'> element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文