I\'m using the bootstrap3-datetimepicker-rails gem to let users store the scheduled_date
of a WorkOrder
in my application (a \'DateTime\' property), bu
This plugin uses moment.js. Basically you need to choose the time format you would like.
example
$(function () {
$('#datetimepicker').datetimepicker({format: 'MMMM Do YYYY, h:mm'});
});
@Uri Agassi is right: it's the values that you're passing to create the WorkOrder
that are the most relevant here.
Try putzing around with the custom date formatting options provided to you by bootstrap-datepicker
. If you can get a format that looks good and is easy to parse on the backend, then you'll want to intercept that parameter before it goes to the model for validation.
Regardless of what you'll go with for date formatting on the client-side, you'll still want to parse it into a useable Date
or DateTime
object server-side in your controller. Check out DateTime#strptime. You can also call #strptime
on Date
.
Untested, but the idea is there:
def create
order_params = work_order_params
order_params[:scheduled_date] = Date.strptime(order_params[:scheduled_date],
'%m/%d/%Y %I:%M %p')
@work_order = WorkOrder.new(order_params)
end
It's me helped:
To file application.js writing to end file:
$(function() {
$('.datepicker').datepicker({format: 'dd-mm-yyyy'});
});
You can remove the modifications from the Controller all together by updating the values in the model. I feel this is a much cleaner (and reusable) approach.
class Work < ActiveRecord::Base
def scheduled_date=(date)
begin
parsed = Date.strptime(date,'%m/%d/%Y %I:%M %p')
super parsed
rescue
date
end
end
end