每天12小时AM / PM范围 [英] Rails 12 hour AM/PM range for a day

查看:284
本文介绍了每天12小时AM / PM范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的问题,可能以前曾有人问过并回答过,但我什么也没找到。

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

无论如何,我需要12小时的时间范围/数组,例如12AM-11 AM、12PM-11PM。您可能会明白要点。现在,我正在尝试做一个荒谬的方法,涉及将AM映射到一个阵列,将PM映射到另一个阵列,然后将两个阵列连接在一起。必须有一种更简单的方法来执行此操作。

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.

我了解Rails time_select ,但是我需要一种与其提供的格式不同的格式。有什么建议么?

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

说明:因此,我要查找的是 12小时制,带有AM和PM。如果我想要24小时制,我可以做(0..24)来完成。但是12个小时的时钟从12到11 AM,然后从12到11 PM。我很确定有人以前这样做过。

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.

推荐答案

我同意@MrYoshi的评论,这是格式化日期的最简单方法是 .strftime()
参见 RubyDoc 获取所有可能的选项

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

示例:

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

输出:HH:MM AM

output: HH:MM AM

或者您真正要求的内容:

Or what you literally asked for:

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

输出:HH:00

正如您提到的 time_select ,我假设您想提供时间作为用户可选范围,因此请尝试使用这些选项进行 time_select 更多选项):

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}

也是上一个问题:用于Rails 3的12小时制的时间选择表单助手?

这篇关于每天12小时AM / PM范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆