rails 3.2中的jQuery datetimepicker格式 [英] jQuery datetimepicker formatting in rails 3.2
问题描述
我正在使用以下datetimepicker,据我所知,它是已经扩展的jquery-ui datepicker。
I'm using the following datetimepicker which from my understanding is the jquery-ui datepicker that has been extended.
http://trentrichardson.com/examples/timepicker/
我的问题是我想要显示给用户上午和下午时间,但我想以不同的方式对其进行格式化,以便当我将其提交到mysql数据库时,时间格式将输入标准的YY-mm-dd - hh:mm:ss格式。
My issue is I want to display to the user am and pm times, but I want to format it differently so that when I submit it to a mysql database, the time format enters the standard "YY-mm-dd - hh:mm:ss" format.
这是它当前输入的格式
推荐答案
对于遇到与此问题相同的人上面,我收到了上述插件的开发人员的一些好建议,格式化的责任应属于中间件(ruby,php,python等)。所以在做了一些搜索后,我找到了以下的gem并添加到我的Gemfile中。
For people with the same issue as the above, I received some good advice from the developer of the above plugin that the responsibility for formatting should belong in the middleware (ruby, php, python, etc). So after doing some searching I found the following gem and added to my Gemfile.
gem 'validates_timeliness'
这个插件依赖于时效性宝石,并且有许多内置函数可以接受许多不同的格式。
This plugin relies on the timeliness gem and has many built in functions that accepts many different formats out of the box.
将此项添加到您的模型中即可完成:
Add this to your model and you're done:
validates_datetime :date_paid_on, :allow_blank => true
您可能遇到可能需要自定义解析器的场景。如果是这样,只需将以下内容添加到validates_timeliness.rb初始值设定项中。
You may run into the scenario where you may need a custom parser. If so just add the following to your validates_timeliness.rb initializer.
config.parser.add_formats(:datetime, 'yyyy-mm-dd hh:nn:ss tz')
这篇关于rails 3.2中的jQuery datetimepicker格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!