20

This is a really simple question, and it's probably been asked and answered before, but I haven't been able to find anything.

Anyway, I need a range/array for 12 hour time, so like 12AM - 11AM, 12PM - 11PM. You probably get the gist of it. Right now I'm trying to do an absurdly complicated method involving mapping AM onto one array, PM onto another one, and then joining the two arrays together. There has to be an easier way to do this.

I know about Rails time_select, but I need a different format than what it provides. Any suggestions?

Clarification: So what I'm looking for is the 12-hour clock, with AM and PM. If I wanted a 24-hour clock, I could just do (0..24), and be done. But the 12-hour clock goes from 12-11 AM, and then goes from 12-11 PM. I'm pretty sure someone has done this before.

irosenb
  • 828
  • 2
  • 13
  • 26
  • Actually, I don't get the gist. :-) The examples you gave are 23 hours apart, if the assumption that the second time in the pair is the next day. What do you mean by "12 hour time"? Also, you said you want yours to look like "HH:00" but that format doesn't match your earlier two examples. Are you looking for just string output? Are you looking for a "range" construct? Are you looking for two Ruby or Rails time-like objects, one for the beginning and end? If you could give an example of the "input" and "output" you'd like to see, it would help. – Peter Alfvin Aug 01 '13 at 17:32
  • 1
    http://apidock.com/ruby/Time/strftime => exemples => "%r - 12-hour time (%I:%M:%S %p)"; in my irb console: `Time.now.strftime("%r") #=> "01:37:01 PM"` – MrYoshiji Aug 01 '13 at 17:37
  • @MrYoshiji I'm not looking for the current time. I'm looking for an array of all the hours in a day, in [12-hour format](https://en.wikipedia.org/wiki/12-hour_clock) – irosenb Aug 01 '13 at 18:18
  • If that is all why not hardcode it as a global or instance variable? It won't seem clean, but it's fast and efficient. What are you planning on using the array for? Maybe you want a hash to map it to the 0-23 hours? – caffeinated.tech Aug 02 '13 at 09:38

3 Answers3

33

I agree with @MrYoshi's comment, the easiest way of formatting a date is .strftime(), see RubyDoc for all possible options

Example:

Time.now.strftime("%I:%M %p")

output: HH:MM AM

Or what you literally asked for:

Time.now.strftime("%I:00")

output: HH:00

As you mentioned time_select I assume you want to offer time as a user selectable range, so try these options for time_select(more options):

time_select 'game', 'game_time', {:minute_step => 60, :ampm => true}

also this previous question: Time select form helper with 12 hour format for Rails 3?

Community
  • 1
  • 1
caffeinated.tech
  • 6,428
  • 1
  • 21
  • 40
7

Rails does this built in

   <%= f.time_select :start,  {ampm: true} %>  

http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-time_select

Will
  • 4,498
  • 2
  • 38
  • 65
  • 2
    It doesn't make any sense to me why it would be 03:PM / 15. This isn't standard - at least, not in the USA. Is this how it is done in other countries? I'd expect it to be 3 / 15 / PM , in 3 select boxes - like any other normal website does it? I know you're not the one writing the Rails code, but this doesn't really solve it for me. – Greg Blass Dec 03 '15 at 15:22
  • I bet there is a config for that, or it is based of your locale. I don't think it lays it out as 03:PM / 15 for me. that looks broken. I'm not in a rails app today or i'd check, sorry @GregBlass – Will Dec 03 '15 at 15:52
  • The question (edited Aug 1st 2013) literally says "I know about Rails time_select, but I need a different format than what it provides." so while interesting, yours is not the sought for answer. – Simon B. Apr 13 '16 at 09:36
6

I know this has already been answered awhile ago with the built in. But I needed a custom function to get these values and wrote this:

times = {"12 AM" => 0}.merge!(1.upto(11).collect { |n| {"#{n} AM" => n} }.reduce(Hash.new, :merge)).merge!({"12 PM" => 12}).merge!(1.upto(11).collect { |n| {"#{n} PM" => n + 12} }.reduce(Hash.new, :merge))

This yields:

{"12 AM"=>0, "1 AM"=>1, "2 AM"=>2, "3 AM"=>3, "4 AM"=>4, "5 AM"=>5, "6 AM"=>6, "7 AM"=>7, "8 AM"=>8, "9 AM"=>9, "10 AM"=>10, "11 AM"=>11, "12 PM"=>12, "1 PM"=>13, "2 PM"=>14, "3 PM"=>15, "4 PM"=>16, "5 PM"=>17, "6 PM"=>18, "7 PM"=>19, "8 PM"=>20, "9 PM"=>21, "10 PM"=>22, "11 PM"=>23}
Aeramor
  • 330
  • 2
  • 10