问题
I'm using the following datetimepicker which from my understanding is the jquery-ui datepicker that has been extended.
http://trentrichardson.com/examples/timepicker/
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.
This is the format that it currently inputs

回答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' } );
回答2:
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
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')
来源:https://stackoverflow.com/questions/12907816/jquery-datetimepicker-formatting-in-rails-3-2