Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 201 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rails 12 hour AM/PM range for a day

#1
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][1], 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.


[1]:

[To see links please register here]

Reply

#2
I agree with @MrYoshi's comment, the easiest way of formatting a date is `.strftime()`,
see [RubyDoc][1] 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][2]):

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

also this previous question:

[To see links please register here]



[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#3
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}
Reply

#4
Rails does this built in

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

[

[To see links please register here]

][1]




[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through