BrunoMiranda.com

Personal Blog about Technology, Software Engineering, Design & More

Displaying 12 hour style time_select in Rails

Posted on June 02, 2007 at 11:20 AM

As most of us already know, if you use the built in time_select helper to interface with a time column in your DB, your HTML select box will display military format. Most of us in the USA are not accustomed to telling time in this format.

After trying a couple of the rails plug-ins out there only to find out none of them worked, I consulted my buddy Rich who wrote a brilliantly simple date helper module to get the job done.

The code follows below:

module ActionView
  module Helpers
    module DateHelper
      def select_hour_with_twelve_hour_time(datetime, options = {})        
        return select_hour_without_twelve_hour_time(datetime, options) 
        unless options[:twelve_hour].eql? true

        val = datetime ? (datetime.kind_of?(Fixnum) ? datetime : datetime.hour) : ''
        if options[:use_hidden]
          hidden_html(options[:field_name] || 'hour', val, options)
        else
          hour_options = []
          0.upto(23) do |hour|
            ampm = hour <= 11 ? ' AM' : ' PM'
            ampm_hour = hour == 12 ? 12 : (hour / 12 == 1 ? hour % 12 : hour)

            hour_options << ((val == hour) ?
              %(\n) :
              %(\n)
            )
          end
          select_html(options[:field_name] || 'hour', hour_options, options)
        end
      end
      alias_method_chain :select_hour, :twelve_hour_time
    end
  end
end

Subversion Source:

http://svn.devjavu.com/bopia/twelve_hour/

Usage:

<%= time_select 'event', 'time', {:twelve_hour => true} %>

Trac:

http://bopia.devjavu.com/projects/bopia/report/1

The above will output something that looks like this:

Time: :

Comments

There are 7 comments on this post. Post yours →


I altered the code to display “12AM” instead of “0AM”, which is more common in my opinion. Here is the code change (to version 4 of the svn repo):

      0.upto(23) do |hour|
        ampm = hour #{ampm_hour}#{ampm}\n) :
          %(#{ampm_hour}#{ampm}\n)
        )
sz

made a slight revision in twelve_hour.rb to get “12AM” vs “0AM”

ampm_hour = hour == 12 ? 12 : (hour / 12 == 1 ? hour % 12 : hour)

becomes

ampm_hour = hour == 0 || hour == 12 ? 12 : (hour / 12 == 1 ? hour % 12 : hour)


Thanks for the plugin. Fixed a bug for 2.1 compatibility. Just need to add html_options{} in the appropriate places ala:

selecthtml(options[:fieldname] || ‘hour’, houroptions.join, options, htmloptions)

Tony

Thanks for making this!

I can’t seem to get this working. I installed the plugin dir into my vendors/plugins dir. Then added the sample code in the view and restarted my server. I just get back the usual military time picker. I am running Rails 2.3.2

Any ideas?

Asyraf

Tony,

rails 2.2 and higher uses a whole bunch of different methods to generate the date select tags. I’ve modified the plugin to support this, but haven’t tested it with rails 2.3.2

i didn’t push it to the svn tho, more familiar with git :)

send me an email if you’d like to try out the plugin with my modifications in it.

Bruno and Rich, Thanks for this!

Asyraf

actually, here:

http://github.com/asyraf9/twelve_hour/tree/master

good luck!


Bruno,

Thank you for posting that link. I actually ended up writing my own time_select. I think you would really like it. The helper only produces ONE select field. I wrote it as a plugin just as you did. Check out my blog post about it here:

http://www.tonyamoyal.com/blog/?p=19

Thanks again for making your plugin! Since I learned a lot from your code, I linked to you on my blog post.

Tony

Post a comment

Required fields in bold.

 

Visit the Archives →