Rails 12 hour AM/PM range for a day

前端 未结 3 1563
眼角桃花
眼角桃花 2021-02-05 13:43

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

3条回答
  •  无人共我
    2021-02-05 14:14

    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?

提交回复
热议问题