Datetime_select 12 hour display? (AM/PM)?

I have been looking all through the documentation and can’t find
anything to display the time in 12 hours format with an AM/PM option. Is
there an option for datetime_select to do this? Or a plugin?

Thanks!

see strftime

http://www.ruby-doc.org/core/classes/Time.html#M000297

Paul H. wrote:

see strftime

http://www.ruby-doc.org/core/classes/Time.html#M000297

That does not help me with the datetime_select function. datetime_select
is a helper that takes options to create select drop downs. It has a
drop down with 0 - 23, which is military time. My users and I would
prefer 12 hour time with AM and PM. I can not figure out how to do this
with the datetime_select helper function. strftime doesnt help

Sorry about that

File vendor/rails/actionpack/lib/action_view/helpers/

date_helper.rb, line 209

It seems as though it only goes from 0-23.

You can define your own select_hour (my_select_hour) and basically:

209: def my_select_hour(datetime, options = {})
210: val = datetime ? (datetime.kind_of?(Fixnum) ? datetime :
datetime.hour) : ‘’
211: if options[:use_hidden]
212: hidden_html(options[:field_name] || ‘hour’, val, options)
213: else
214: hour_options = []
215: 0.upto(23) do |hour|
216: hour_options << ((val == hour) ?
217: %(#{leading_zero_on_single_digits(hour)}</
option>\n) :
218: %(#{leading_zero_on_single_digits(hour)}\n)
219: )
220: end
221: select_html(options[:field_name] || ‘hour’,
hour_options, options)
222: end
223: end

Change 217/218 to read something like
%(#{ (hour == 0 ? 12 : ( hour > 12 ? hour - 12 :
hour )) } #{( hour >= 12 ? “PM” : “AM”)}\n) :
%(>#{ (hour ==
0 ? 12 : ( hour > 12 ? hour - 12 : hour )) } #{( hour >= 12 ? “PM” :
“AM”)}\n)

I have been looking all through the documentation and can’t find
anything to display the time in 12 hours format with an AM/PM option. Is
there an option for datetime_select to do this? Or a plugin?

http://agilewebdevelopment.com/plugins/search?search=12+hour

Perhaps the top two would do the trick?

Of all the things rails does, it can’t do a 12 hour time drop down.
Thanks for your help!

<%= my_hour_select(‘event[start_time]’ %>

Should read: <%= my_hour_select(‘event[start_time]’) %>

Cheers

Came across this post and wanted to add my own solution, which isn’t
better than the above mentioned ones, just another option (also doesn’t
require losing the original rails helper and/or installing another
plugin).

in application_helper.rb or something:

def my_hour_select
str =
select_tag(‘hour’,options_for_select([1,2,3,4,5,6,7,8,9,10,11,12])
end

then in your view:

<%= my_hour_select %>

You could use the ‘selected’ option from options_for_select and
Time.now.hour to select the current hour if needed.

You can then set the name of the select to match a column in your model
for re-usability by adding an option to the method like so:

def my_hour_select(field_name)
str =
select_tag(field_name,options_for_select([1,2,3,4,5,6,7,8,9,10,11,12])
end

and in your view:

<%= my_hour_select(‘event[start_time]’ %>

Cheers,
divotdave

Ben J. wrote:

I have been looking all through the documentation and can’t find
anything to display the time in 12 hours format with an AM/PM option. Is
there an option for datetime_select to do this? Or a plugin?

Thanks!

Ben,

Any luck on a solution to your post? I’m also having the same problem.

Thanks,
John

Ben J. wrote:

I have been looking all through the documentation and can’t find
anything to display the time in 12 hours format with an AM/PM option. Is
there an option for datetime_select to do this? Or a plugin?

Personally, I think of the Rails date_select helpers in a similar way I
think of scaffolding. It’s fine for “quick-n-dirty” entry of dates and
times, but for a “real” application go find a decent calendar widget.
One that supports entry of both date and time in a more intuitive and
user friendly way.

Here’s one example:
http://github.com/timcharper/calendar_date_select (Demo:
http://electronicholas.com/calendar)

Robert W. wrote:

Ben J. wrote:

I have been looking all through the documentation and can’t find
anything to display the time in 12 hours format with an AM/PM option. Is
there an option for datetime_select to do this? Or a plugin?

Personally, I think of the Rails date_select helpers in a similar way I
think of scaffolding. It’s fine for “quick-n-dirty” entry of dates and
times, but for a “real” application go find a decent calendar widget.
One that supports entry of both date and time in a more intuitive and
user friendly way.

Here’s one example:
http://github.com/timcharper/calendar_date_select (Demo:
http://electronicholas.com/calendar)

Thanks! I like the date and time option with the calendar widget.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs