1

我正在使用以下 datetimepicker,据我了解,它是已扩展的 jquery-ui datepicker。

http://trentrichardson.com/examples/timepicker/

我的问题是我想向用户显示 am 和 pm 时间,但我想对其进行不同的格式化,以便当我将其提交到 mysql 数据库时,时间格式输入标准“YY-mm-dd - hh:mm: ss”格式。

这是它当前输入的格式

时间格式

4

2 回答 2

1

jQuery UI date pickers support passing altField and altFormat options to them for exactly this purpose. The plugin you are using also supports extending that (although in a confusing way). I believe this is the correct level of options that will get you what you need. Try it out live here

Assuming a form with these elements:

<input type="text" id="datetime" />
<input type="hidden" id="datetime_to_server" name="formatted_date_time" />

You can then set up your datetimepicker widget like this:

$('#datetime').datetimepicker( 
  { altField  : '#datetime_to_server',  // selector of the hidden input field you want sent to the server
    altFormat : 'yy-m-d',
    altFieldTimeOnly : false,
    altTimeFormat : 'hh:mm:00' } );
于 2012-10-16T05:26:12.547 回答
0

对于与上述相同问题的人,我从上述插件的开发人员那里得到了一些很好的建议,即格式化的责任应该属于中间件(ruby、php、python 等)。因此,经过一番搜索,我找到了以下 gem 并添加到了我的 Gemfile 中。

gem 'validates_timeliness'

该插件依赖于及时性 gem,并具有许多内置功能,可以开箱即用地接受许多不同的格式。

将此添加到您的模型中,您就完成了:

validates_datetime :date_paid_on, :allow_blank => true

您可能会遇到需要自定义解析器的场景。如果是这样,只需将以下内容添加到您的 validates_timeliness.rb 初始化程序中。

config.parser.add_formats(:datetime, 'yyyy-mm-dd hh:nn:ss tz')
于 2012-10-20T20:00:09.063 回答